I am trying to hide a few pages from WordPress Search results. For that, I have written a code.
function jp_search_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 10,11,20,105 ) );
}
}
add_action( 'pre_get_posts', 'jp_search_filter' );
However, this code doesn't seem to be working. The pages that I wanted to hide from WordPress search results are still showing. What I have done in the above code is I passed the page IDs as an array.
Can anyone help me with the code? Thank you so much.
My Answer:
To hide a specific page from WordPress search results, you can use the following code:
php
function jp_search_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 10 ) ); // Replace 10 with the ID of the page you want to hide
}
}
add_action( 'pre_get_posts', 'jp_search_filter' );
In this code, you just need to replace the number 10 with the ID of the specific page you want to hide from search results. This code should work to hide that specific page from search results.
Rate this post
3 of 5 based on 8492 votesComments