Exclude posts from WordPress home page based on their category
There may be situations where we want to exclude posts from the WordPress home page based on their category.
To get this done, first we want to find template file for home page. Very often, it will be index.php
in our theme folder. Then find the below line of code on it.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Just replace this line with below code.
<?php query_posts('cat=-4'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
This will exclude posts in category number four from being displayed on the home page.
Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!
Recent Posts
- Automating Code Linting with GitHub Actions for WordPress Plugins
- Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer
- The Ultimate Guide to Indexing in Database Design
- Understanding 'update_meta_cache' in WordPress - When to Use It, When Not to, and Its Impact on Database Queries
- A Guide to Configuring JavaScript and SCSS Paths in WordPress Plugins with @wordpress/scripts
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about it, please get in touch.