Tagged: ajax, exclude pages, search
-
AuthorPosts
-
June 16, 2015 at 12:49 pm #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
June 16, 2015 at 4:31 pm #460133It 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 ?
June 16, 2015 at 6:02 pm #460230Hi!
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ünterJune 16, 2015 at 7:13 pm #460296That’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 resultsPlease 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, 5 months ago by IndikatorDesign. Reason: Config the search for forums also
June 17, 2015 at 6:40 am #460562June 9, 2017 at 4:27 pm #806127Excellent! 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!
June 11, 2017 at 7:11 am #806479Hi @jstonestreet,
If you edit a post in that post type then you should see it in the URL as a parameter.
Best regards,
RikardJune 12, 2017 at 7:00 pm #807018That’s correct. Thanks for pointing that out!
June 13, 2017 at 5:33 am #807223Hi,
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,
SarahJune 27, 2017 at 12:09 pm #813368Hello 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,
NickJune 29, 2017 at 5:38 am #814215Hi,
Please remove the following line.
, ‘your_custom_post_type’
Best regards,
IsmaelJune 30, 2017 at 7:23 pm #815123Hi 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,
NickJuly 1, 2017 at 7:11 am #815265Hi,
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 -
AuthorPosts
- You must be logged in to reply to this topic.