Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #460007

    Hey there, hope you’re well!

    I excluded certain pages and posts from our search index with the following code:

    if ( ! function_exists( 'bb_filter_search_results' ) )
    {
    	add_action( 'pre_get_posts', 'bb_filter_search_results' );
    	function bb_filter_search_results( $query )
    	{
    		if ( ! $query->is_admin && $query->is_search )
    		{
    			$query->set('post__not_in', array({page-ids}) );
    		}
    		return $query;
    	}
    }

    Please how can I exclude this pages and post also from ajax search in nav-bar by id (without an extra plugin like relevanssi)

    Thank you and best regards

    #460133

    It seems that is needed to manipulate “function avia_ajax_search()” inside functions-enfold.php on line 131 –>

    // $defaults = array('numberposts' => 5, 'post_type' => 'any', 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false);
    $defaults = array('numberposts' => 5, 'post_type' => array( 'post', 'portfolio', 'document' ), 'post__not_in' => array( {post-ids comma separated} ), 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false);

    This works, but is there a better way, perhaps with a filter ?

    #460230

    Hi!

    Thank you for using our theme.

    Have a look at line 153:

    
    $search_query 		= apply_filters('avf_ajax_search_query', http_build_query($search_parameters));
    

    You can manipulate the query parameters with the filter avf_ajax_search_query.

    Hope, this helps you.

    Regards,
    Günter

    #460296

    That’s it.. thanks Günter!

    If someone else need it, this is the complete code to add it to your childtheme’s functions.php to change search results for normal search index and ajax search results (navbar):

    // Use search just for certain post-types and exclude posts by id
    if ( ! function_exists( 'bb_filter_search_results' ) )
    {
    	add_action( 'pre_get_posts', 'bb_filter_search_results' );
    	function bb_filter_search_results( $query )
    	{
    		if ( ! $query->is_admin && $query->is_search )
    		{
    			$query->set( 'post_type', array( 'post', 'portfolio', 'document', 'your_custom_post_type' ) );
    			$query->set( 'post__not_in', array(1,2,3,4,5) );
    		}
    		return $query;
    	}
    }
    
    if ( ! function_exists( 'bb_filter_ajax_search_results' ) )
    {
    	add_filter('avf_ajax_search_query', 'bb_filter_ajax_search_results', 10, 1);
    	function bb_filter_ajax_search_results( $search_parameters )
    	{
    		$defaults = array('numberposts' => 5, 'post_type' => array( 'post', 'portfolio', 'document', 'your_custom_post_type' ), 'post__not_in' => array(1,2,3,4,5), 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false);
    		$_REQUEST['s'] = apply_filters( 'get_search_query', $_REQUEST['s']);
    		$search_parameters = array_merge( $defaults, $_REQUEST );
    		return $search_parameters;
    	}
    }

    For both functions you have to change the values to your needs inside the arrays for “post_type” and “post_not_in”.

    post_type –> include the post-types you wanna include to your search results
    post_not_in –> exclude the posts by id you don’t want in your search results

    Please note, that pages are not included. For this you must add – ‘page’ – to post_type.

    If you use bbPress and ajax search form form navbar for forum-search, you have to change this:

    if ( ! $query->is_admin && $query->is_search )

    with this:

    if ( ! strpos($_SERVER["REQUEST_URI"], 'forums') && ! strpos($_SERVER["REQUEST_URI"], 'bbp_search') && ! $query->is_admin && $query->is_search )

    If you use another slug than “forums”, than you have to change it to your slug.

    Thank you and best regards
    Bruno

    • This reply was modified 9 years, 6 months ago by IndikatorDesign. Reason: Config the search for forums also
    #460562

    Hey Bruno,

    Thanks for sharing!

    Regards,
    Rikard

    #806127

    Excellent! Thanks for sharing this solution! Works perfectly!

    One question… where can I find the name of a custom post type? I figured it out by trial and error, but I’m sure there’s an easier way that will save people time.

    It would also be a nice theme feature to be able to select the custom post types I want displayed or hidden from the ajax search box and the search results page.

    Thanks!

    #806479

    Hi @jstonestreet,

    If you edit a post in that post type then you should see it in the URL as a parameter.

    Best regards,
    Rikard

    #807018

    That’s correct. Thanks for pointing that out!

    #807223

    Hi,

    We’re glad Rikard was able to help you. Please let us know if you need help with anything else about this topic.

    Best regards,
    Sarah

    #813368

    Hello Sirs,

    I am trying also to exclude some posts and pages from search results and for testing purposes, I used the post-page with id:5048 that I want to exclude.

    Now, my search doesn’t display any result…

    I used the following code to my functions.php file:

    // Use search just for certain post-types and exclude posts by id
    if ( ! function_exists( ‘bb_filter_search_results’ ) )
    {
    add_action( ‘pre_get_posts’, ‘bb_filter_search_results’ );
    function bb_filter_search_results( $query )
    {
    if ( ! $query->is_admin && $query->is_search )
    {
    $query->set( ‘post_type’, array( ‘post’, ‘portfolio’, ‘document’, ‘your_custom_post_type’ ) );
    $query->set( ‘post__not_in’, array(5048) );
    }
    return $query;
    }
    }

    if ( ! function_exists( ‘bb_filter_ajax_search_results’ ) )
    {
    add_filter(‘avf_ajax_search_query’, ‘bb_filter_ajax_search_results’, 10, 1);
    function bb_filter_ajax_search_results( $search_parameters )
    {
    $defaults = array(‘numberposts’ => 5, ‘post_type’ => array( ‘post’, ‘portfolio’, ‘document’, ‘your_custom_post_type’ ), ‘post__not_in’ => array(5048), ‘post_status’ => ‘publish’, ‘post_password’ => ”, ‘suppress_filters’ => false);
    $_REQUEST[‘s’] = apply_filters( ‘get_search_query’, $_REQUEST[‘s’]);
    $search_parameters = array_merge( $defaults, $_REQUEST );
    return $search_parameters;
    }
    }

    I did something wrong?

    I am waiting for your answer…

    Thank You in advance.

    Best regards,
    Nick

    #814215

    Hi,

    Please remove the following line.

    , ‘your_custom_post_type’ 
    

    Best regards,
    Ismael

    #815123

    Hi Ismael,

    Thank you for your excellent support!

    Unfortunately it didn’t work. It doesn’t display any result as you see in screenshot: https://prnt.sc/fq3lk2

    I really appeciate your help,

    Best regards,
    Nick

    #815265

    Hi,

    It is working on my end. Please try this code instead.

    // Use search just for certain post-types and exclude posts by id
    if ( ! function_exists( 'bb_filter_search_results' ) )
    {
    	add_action( 'pre_get_posts', 'bb_filter_search_results' );
    	function bb_filter_search_results( $query )
    	{
    		if ( ! $query->is_admin && $query->is_search )
    		{
    			$query->set( 'post_type', array( 'post', 'portfolio', 'document' ) );
    			$query->set( 'post__not_in', array(5048) );
    		}
    		return $query;
    	}
    }
    
    if ( ! function_exists( 'bb_filter_ajax_search_results' ) )
    {
    	add_filter('avf_ajax_search_query', 'bb_filter_ajax_search_results', 10, 1);
    	function bb_filter_ajax_search_results( $search_parameters )
    	{
    		$defaults = array('numberposts' => 5, 'post_type' => array( 'post', 'portfolio', 'document' ), 'post__not_in' => array(5048), 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false);
    		$_REQUEST['s'] = apply_filters( 'get_search_query', $_REQUEST['s']);
    		$search_parameters = array_merge( $defaults, $_REQUEST );
    		return $search_parameters;
    	}
    }
    

    NOTE: Please copy the code directly from this forum and not from your email.

    Best regards,
    Ismael6

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.