Exclude categories from main page question (2 posts)

  1. zefs
    Posted 3 years ago #

    Hello, I am using the following function to exclude posts(categories) from the main page of my blog.

    function my_exclude_category( $query ) {
      if ( $query->is_home ) {
    		$query->set( 'cat', '-3');
    	return $query;
    add_filter( 'pre_get_posts', 'my_exclude_category' );

    It works great, but I want it to work only if I have selected 1 category(when creating the post) so:

    If the post is on Videos category(id = cat3) I want it to be hidden, but if I check both Video category and General when making the post, it should be visible.

    Is that possible?

  2. zefs
    Posted 3 years ago #

    Not sure if I described that correctly, here's what I am trying to do:

    if ( $query->in_category(-3) && in_category(-7) ){
         //display the posts that are included in these two categories(3,7)
    //else if they are in any of each of these categories(not both)hide them
    else { $query->set( 'cat', '-3 -7');}

Topic Closed

This topic has been closed to new replies.

About this Topic