how to exclude category from ajax search

Posted in: Newspaper
Post count: 11

hi i wanna exclude 1 category from wp search with this code

and work good but still show in ajax searching
please help me

/* Exclude a Category from Search Results */

add_filter( ‘pre_get_posts’ , ‘search_exc_cats’ );
function search_exc_cats( $query ) {

if( $query->is_admin )
return $query;

if( $query->is_search ) {
$query->set( ‘category__not_in’ , array( 30 ) ); // Cat ID
}
return $query;
}

  • This topic was modified 9 years by SirBabak.
Post count: 22421

Hello,

Please follow this topic:
https://forum.tagdiv.com/topic/search-box-3/

Thank you!

Post count: 11

yes but imean when searching and before hit enter in drop down ajax seach show it

Post count: 22421

Hello,

We have not tested this but you can alter these file if you have the proper knowledge:

http://screencast.com/t/uIiRjFub13 and http://screencast.com/t/Jm4uyHbRP6b

Thanks

Post count: 9

Add this code in functions.php
Works fine for me!

function excludeCatsFromSearchResults( $query ) {
     if ( $query->is_search ) {
          $query->set( 'cat', '-xx,-xx' ); // replace "xx" with your category-id
     } return $query;
} add_action( 'pre_get_posts', 'excludeCatsFromSearchResults' );
Viewing 5 posts - 1 through 5 (of 5 total)
The forum ‘Newspaper’ is closed to new topics and replies.