Blog Archive Template: Exclude/Include Categories

Hi there - I am using Integrity/child, and looking for the code that governs the blog archive page (/blog/) so that I can add a little php that allows me to either limit or include categories by category ID.

the purpose is to exclude a category of posts I don’t want showing up on /blog/. So if there’s another way to approach it, that’s fine too. Thanks! Tyler

Hi Tyler,

Please add the following code under functions.php file locates in your child theme:

function x_exclude_cat_from_blog($query) {
	if ($query->is_home() && $query->is_main_query()) {
		$query->set('cat', '-123,-456');
	}
}
add_action('pre_get_posts', 'x_exclude_cat_from_blog');

Replace the 123, 456 numbers with your category IDs.

Let us know how it goes!

1 Like

This worked perfectly. Thank you so much, @thai!

Hi @tribalcore,

Happy to hear that.

Feel free to ask us again.

Thanks :smiley: