Exclude specific category, post, page from Search in WordPress ?

If you want to exclude a specific category or post or page then you have to put the id in below code.

function exclude_category_search( $query ) {
  if ( is_admin() || ! $query->is_main_query() )
    return;

  if ( $query->is_search ) {
    $query->set( 'post__not_in', array( 668,5004,5409,5407,4392,5416,500,502,3817,3819,4708,671) );
    $query->set( 'cat', '-3625,-40' );
  }

}
add_action( 'pre_get_posts', 'exclude_category_search', 1 );
Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *