Dade Don
Dade Don

Reputation: 3

How to exclude categories from WordPress blog post page

I'm using a static page for my front page and another page for my blog post.

How can i exclude certain categories from showing up on my blog page. I've seen the code for the front page but can't get it to work for my blog page

function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-4,-5,-6,-7,-8,-10,-12,-13' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );

Upvotes: 0

Views: 189

Answers (2)

Raunak Gupta
Raunak Gupta

Reputation: 10809

It's strange your code should have worked try this way.

function exclude_category($query)
{
    if ($query->is_home() && $query->is_main_query())
    {
        //Alternative method
        $query->set('category__not_in', array(4, 5, 6, 7, 8, 10, 12, 13));
    }
}

add_action('pre_get_posts', 'exclude_category');

Hope this helps!

Upvotes: 0

Prakash Singh
Prakash Singh

Reputation: 651

add_action( 'pre_get_posts', 'exclude_category_posts' );

function exclude_category_posts( $query ) {
if( $query->is_main_query() && $query->is_home() ) {
$query->set( 'cat', '-359, -2' );
    }
}

Try this by putting in your functions.php

Upvotes: 1

Related Questions