Usually, we use a semi-static webpage for WordPress home. It is a quite natural fact that we want to highlight posts from certain categories on the WordPress home page. For various reasons, we need to publish a few articles “irrelevant” to the site’s niche. For the sake of SEO, we should try to exclude such posts from our WordPress home page.
In case you want the sponsored posts not to appear on your home page for a few days, then you should create a new category to temporarily show the posts. You have to manually move to the “vanishing category” after a certain time so that the home archive does not have the articles.
How to Exclude Posts from a Particular Category
First, there is an excellent plugin named “Ultimate Category Excluder”. You can search for this plugin from your WordPress “Add New” plugin page or visit the official repository at WordPress.ORG :
---
1 | https://wordpress.org/plugins/ultimate-category-excluder/ |
All you have to do is, activate the plugin and navigate to the settings page of the plugin. Usually, this is the last part of the URL:
1 2 3 | /wp-admin/options-general.php?page=ultimate-category-excluder.php ## example # https://example.com/wp-admin/options-general.php?page=ultimate-category-excluder.php |
Use this page to select the categories you wish to exclude and where you would like to exclude them from such as home page, feed, all archives, search etc. Remember that this will not vanish the articles from the search engine result page. This plugin is slightly a bigger hammer for our needs.
Snippets to Exclude Posts from a Particular Category
If you add the below snippet to your theme’s functions.php
file, it will vanish the articles from the category you’ll mention from WordPress homepage:
1 2 3 4 5 6 7 8 | function exclude_category_home( $query ) { if ( $query->is_home ) { $query->set( 'cat', '-2' ); } return $query; } add_filter( 'pre_get_posts', 'exclude_category_home' ); |
Please notice this line:
1 | $query->set( 'cat', '-2' ); |
Here we have mentioned the category as minus 2. If you want to exclude category 15, then you should mention as minus 15 :
1 2 3 4 5 6 7 8 | function exclude_category_home( $query ) { if ( $query->is_home ) { $query->set( 'cat', '-15' ); } return $query; } add_filter( 'pre_get_posts', 'exclude_category_home' ); |
If you want to vanish articles from multiple categories, then you can simply add then after a comma mark:
1 2 3 4 5 6 7 8 | function exclude_category_home( $query ) { if ( $query->is_home ) { $query->set( 'cat', '-15, -2, -10' ); } return $query; } add_filter( 'pre_get_posts', 'exclude_category_home' ); |
Remember that the below format is incorrect :
1 | 'cat', '-15', '-2', '-10' |
In our one old article, we mentioned about a plugin to avoid adding snippets on theme‘s functions.php
file.
You can use that plugin to add the above code. Also, I have kept a ready to use plugin at GitHub repository for your testing.