WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Query posts to exclude a category in the search results

[Resolved] Query posts to exclude a category in the search results

  • Hello everyone,

    I tried using the template tag query_posts(“cat=-7”);
    to make it so my search results did not get that category id 7, however now the search results don’t really work it just searches one category( it seems) and doesn’t have pagination. I am new to PHP but understand a little.

    Does anyone have any idea or know how to accomplish what I am looking for.

    Thanks,
    Aaron

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try this instead.

    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( 1, 2, 3 ) ); // Example multiple cats
    		$query->set( 'category__not_in' , array( 1 ) ); // Single cat
    	}
    	return $query;
    }

    In your functions.php (of your theme) is fine.

    Thanks that worked perfect for me. Thanks for the snippit of code.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Query posts to exclude a category in the search results’ is closed to new replies.
Skip to toolbar