Create blog showing only select categories

Hi -

How do I set up a blog that shows only a select group of categories? I would like it to show all categories except one.

Thanks!
Jennifer

Hello there,

You could use normal blog page which is set in the Settings > Reading > Posts page. In order to exclude one category, you would need to add this function to your child theme’s functions. Or use a plugin that allows PHP code addition like Code Snippets.

function astrid_child_filter_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {

      $slugs   = array();
      $slugs[] = 'uncategorized';

      $taxquery = array(
        array(
            'taxonomy' => 'category',
            'field' 		=> 'slug',
            'terms' 		=> $slugs,
            'operator' => 'NOT IN'
        )
      );

      $query->set( 'tax_query', $taxquery );

    }
}
add_action( 'pre_get_posts', 'astrid_child_filter_category' );

Edit this line:

 $slugs[] = 'uncategorized';

and replace uncategorized with the category slug you want to exclude.

Regards,
Kharis

1 Like