WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Query posts to exclude a category in the search results (3 posts)

  1. pengume
    Member
    Posted 4 years ago #

    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

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  3. pengume
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic