How to Exclude a Category from WordPress Blog Page via Plugin or Code

>How to Exclude a Category from WordPress Blog Page via Plugin or Code
  • exclude category

Recently, I did some pro bono work and set up a quick and simple WordPress website for a Fiverr freelancer who really needed it.

Since he’s a content writer, he wants to add some sample writings as a portfolio.

Since those consist of various articles, it’s best to use the posting/blog system, not a page. So, I created a category called “Samples”.

But, as you might already know, what you add in a category also shows on the main WordPress blog page, which in cases like this one is not ideal.

So, in this tutorial, I’ll show you how to exclude category posts from your WordPress blog page using a code or a plugin!

This will also work if your homepage displays your latest posts, making it your blog page, basically.

Exclude a category from your WordPress blog page using code

Here’s what you need to do.

Step 1

You’ll need to find the ID of the category that you want to exclude from your WordPress blog page.

Step 2

Access your WordPress website’s files and find the functions.php file. That’s where you’ll add the code.

edit WordPress functions.php cpanel

Editing functions.php via cPanel

It should normally be found in:

/public_html/wp-content/themes/YOURTHEME-child/functions.php.

If you want to edit the file on add-on domain, not your main one, then it should normally be in:

/public_html/YOURDOMAIN.COM/wp-content/themes/YOURTHEME-child/functions.php

I recommend backing up the file or the whole website, and also having a child theme in place, otherwise, your changes will be lost with the next theme update.

Step 3

Add the following PHP code right at the very bottom of the functions.php file, or right before the ?> closing tag (if there’s any).

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

exclude category blog page wordpress

Note that you have to replace the category ID ('-7' in my example) with your own.

If you want to exclude multiple categories from your blog page in WordPress, then add more IDs in $query->set. For example:

function exclude_category_posts( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-7, -12, -15' );
}
}
add_action( 'pre_get_posts', 'exclude_category_posts' );

Again, don’t forget to replace the category IDs with your own!

Exclude a category from your WordPress blog page using a plugin

Even though this is the easiest method, I strongly recommend going with the first one because the fewer plugins you have, the better!

Step 1

Install the Ultimate Category Excluder plugin.

Here’s how to install a WordPress plugin.

Step 2

Go to the plugin’s options in Settings -> Category Excluder.

ultimate category excluder wordpress plugin

As you can see, it lets you exclude categories from different places in WordPress:

  • Front page;
  • Feeds;
  • All archives;
  • Search.

All you have to do is to check one or multiple boxes, click the Update button, and the WordPress categories will be removed from the selected places.

That’s a wrap

I hope you found the post useful and comprehensive, and you managed to successfully exclude a category from your blog page in WordPress!

Don’t forget to share the post to help out others!

You can quickly subscribe to my newsletter by using this link.

If you have any questions or thoughts, drop a comment or send a message via contact or Facebook page.

You can also hit the follow button on Twitter and subscribe to the YouTube channel.

My WordPress website or blog installation service is at your disposal!

On-going WordPress support and maintenance services are on the menu as well!

By |2018-10-31T00:07:37+00:00October 31st, 2018|Customization, Plugins|0 Comments

Leave A Comment

WP ENGINE OFFER!

20% OFF + 4 months FREE on WordPress hosting
Offer expires on 11/30/2018
To celebrate the release of the Gutenberg editor, WP Engine is offering:
  • 20% OFF the first payment on their shared WordPress hosting plans;
  • 4 months FREE on any annual subscription.

And there's more! Since WP Engine bought StudioPress, all their customers get FREE access to the StudioPress themes, including the Genesis Framework!
 
Disclosure: If you make a purchase using this affiliate link, I might receive a commission from WP Engine, at no additional cost to you! Thank you!
close-link