Site logo

Archived topic

filter category on blog

3 replies · Started by Jusung on February 1, 2023

Viewing posts 1–4 of 4

Hello!

I have made a couple of categories on my site.

I don't wanna show posts in the categories that starat with po and All.

Is there a way to remove certain category in blog page?

Hi there,

you can use the pre_get_posts hook in a PHP Snippet like so:

add_action('pre_get_posts', 'exclude_category_posts');
function exclude_category_posts( $query ) {
    if( $query->is_main_query() && !is_admin() && !is_singular() ) {
        $query->set('cat', array( -22, -33 ));
    }
}

In this line you set the IDs of the categories you want to exclude:

$query->set('cat', array( -22, -33 )); make sure to keep the - minus before each Id.

Thank you!! it works!

Glad to hear that!

This archived topic is closed to new replies.