Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1269471

    Hi,

    On the Enfold menu search is it possible to filter it so it only searches certain taxonomys e.g. it only searches posts?

    Thanks,
    Danny

    #1269496

    Hi Danny,

    Add these codes in functions.php:

    For search results page:

    function exclude_pages_from_search($query) {
        if ( $query->is_main_query() && is_search() ) {
            $query->set( 'post_type', 'post' );
        }
        return $query;
    }
    add_filter( 'pre_get_posts','exclude_pages_from_search' );

    For dropdown search result:

    function avf_modify_ajax_search_query($search_parameters) {
    	parse_str($search_parameters, $params);
    	$params['post_type'] = 'post';
    	$search_parameters = http_build_query($params);
    	return $search_parameters;
    }
    
    add_filter('avf_ajax_search_query', 'avf_modify_ajax_search_query', 10, 1);

    Hope it helps :)

    Best regards,
    Nikko

    #1269728

    Hi

    Thanks for that, do I need to add both sections, what is the dropdown search results?

    Thanks
    Danny

    #1269866

    Hi Danny,

    Yes, you need both. The dropdown is the search results that come like a preview to a search field in your header.

    Best regards,
    Victoria

    #1271315

    Hi

    Thanks this has worked but one thing I have noticed now is in admin when searching pages for example, it brings up posts as results now, is there a way round this?

    Thanks,
    Danny

    #1271472

    Hi Danny,

    Do you want to give different results depending on the user role?
    If yes, then you can use if-else conditions then base it on Roles and Capabilities: https://wordpress.org/support/article/roles-and-capabilities/
    Otherwise, you need to filter it based on the page where the user is on then use Conditional Tags: https://codex.wordpress.org/Conditional_Tags

    Best regards,
    Nikko

    #1271775

    Hi,

    Sorry I might not have worded right, what I mean is the search on the front end is working fine now. The issue we have now is that this has also changes the search in the backend/admin area so when you go into the pages section where you would add a page and search for something in there, that is also only bringing posts up to edit instead of pages, I wasn’t expecting this to happen,I just wanted the front end search for the user to only search posts.

    Thanks,
    Danny

    #1272060

    Hi Danny,

    I see, please replace the codes I gave with the following:

    For search results page:

    function exclude_pages_from_search($query) {
        if ( $query->is_main_query() && is_search() && !is_admin() ) {
            $query->set( 'post_type', 'post' );
        }
        return $query;
    }
    add_filter( 'pre_get_posts','exclude_pages_from_search' );

    For dropdown search result:

    function avf_modify_ajax_search_query($search_parameters) {
    	if ( !is_admin() ) {
    		parse_str($search_parameters, $params);
    		$params['post_type'] = 'post';
    		$search_parameters = http_build_query($params);
    	}
    	return $search_parameters;
    }
    
    add_filter('avf_ajax_search_query', 'avf_modify_ajax_search_query', 10, 1);

    Best regards,
    Nikko

    #1272611

    Hi

    Thanks for that, it’s nearly worked, the search is fine now in admin and on the results page when not logged in brings up only posts so that is great, on the dropdown results though it is now brining up all the taxonmys now, is there someone that needs tweaking in that part?

    Thanks,
    Danny

    #1272731

    Hi Danny,

    Can you give us a link to your site? and also some keywords to search to test this.

    Best regards,
    Nikko

    #1272880

    Hi,

    Yes no problem, see info in the box.

    Thanks,
    Danny

    #1273195

    Hi Danny,

    Thanks, can you try to remove the conditional statement from this code I gave:

    function avf_modify_ajax_search_query($search_parameters) {
    	if ( !is_admin() ) {
    		parse_str($search_parameters, $params);
    		$params['post_type'] = 'post';
    		$search_parameters = http_build_query($params);
    	}
    	return $search_parameters;
    }
    
    add_filter('avf_ajax_search_query', 'avf_modify_ajax_search_query', 10, 1);

    so it should be (just like the previous):

    function avf_modify_ajax_search_query($search_parameters) {
    	parse_str($search_parameters, $params);
    	$params['post_type'] = 'post';
    	$search_parameters = http_build_query($params);
    	return $search_parameters;
    }
    
    add_filter('avf_ajax_search_query', 'avf_modify_ajax_search_query', 10, 1);

    Then test if it works properly, then also test if it affects the backend.

    Best regards,
    Nikko

    #1273255

    That’s done the job thank you :)

    #1273264

    Hi dannyl82,

    We’re glad to hear that :)
    Thanks for using Enfold and have a great day!

    Best regards,
    Nikko

Viewing 14 posts - 1 through 14 (of 14 total)
  • The topic ‘Menu Search Only Posts’ is closed to new replies.