Support » Requests and Feedback » Exclude category from blog page

  • Resolved neallos

    (@neallos)


    Hi,

    I want to exclude a post category from my blog page. I’m using Qode Bridge, in which the theme does not have this function by itself. So I’m trying to get this done with coding, but can not manage to get this working.

    I want to exclude category #31, only on the page ‘blog’. How can I manage to do so?

    function exclude_category( $query ) {
    if ( $query->is_single ($post = 'blog' ) ) {
    $query->set( 'cat', '-31' );
    }
    return $query;
    }
     
    add_filter( 'pre_get_posts', 'exclude_category' );

    Thanks,

    Neal

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @neallos,

    Neal, you have the code mostly correct. It needs to use is_home, which is the conditional tag required for the blog page: https://developer.wordpress.org/reference/functions/is_home/

    Could you please try the following code instead:

    // Remove category 31 from blog page
    function exclude_category_blogpage( $query ) {
    	if ( $query->is_home() ) {
    		$query->set( 'cat', '-31' );
    		}
    	return $query;
    }
     
    add_filter( 'pre_get_posts', 'exclude_category_blogpage' );

    Niall

    Joy

    (@joyously)

    That code will never work.
    You have the correct idea, though.
    I haven’t tested it, but perhaps you could remove
    $query->is_single ($post = 'blog' )
    and replace it with
    $query->is_home() this will match the blog list of posts whether it’s on the home page or any other page
    or
    $query->is_page('blog') this will match the Page named ‘blog’ whether it is being used as the page for posts or not

    neallos

    (@neallos)

    Great! Thanks @nm1com and @joyously for your responses, that did work fabulously!

    You’re welcome! Glad the solutions worked!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.