Introduction: Why Avoid Plugins?
In WordPress development, we often need to implement specific features, such as excluding posts from certain categories on the homepage. While plugins can provide quick solutions, over-reliance on them can cause issues:
- Performance Impact: Plugins add PHP execution time, slowing down your site.
- Reduced Stability: Plugins may conflict with each other or contain vulnerabilities, affecting site stability.
- Increased Database Load: Some plugins run frequent database queries, adding server strain.
Therefore, for common requirements like this, implementing via code is often better. Before modifying theme files, always back them up and document your changes for easy recovery.
Method 1: Exclude Within the Loop
This method checks inside the post loop and skips posts from the specified category.
Steps:
- Open your theme's
index.phpfile. - Find the
if (have_posts())orwhile (have_posts())statement. - Add this code inside the loop, typically after
the_post();:
<?php
// Skip posts in category ID 12 on the homepage
if (in_category(12) && is_home()) {
continue;
}
?>
Notes:
- Replace
12with your category ID. - This is simple but may cause fewer posts to display if many are skipped.
Method 2: Use query_posts Function (Recommended)
This method modifies the main query to exclude categories before fetching posts.
Steps:
- Open
index.php. - Add this code before
if (have_posts())orwhile (have_posts()):
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'category__not_in' => array(12), // Exclude category ID 12
'paged' => $paged
);
query_posts($args);
?>
Explanation:
category__not_in: WordPress query parameter to exclude categories.paged: Preserves pagination.- This modifies the main query directly.
Method 3: Modify Query String
A concise alternative using query_posts with a query string.
Steps:
- Open
index.php. - Find the line with
if (have_posts()) : while (have_posts()) : the_post();. - Replace it with:
<?php if (have_posts()) : query_posts($query_string . '&cat=-12'); while (have_posts()) : the_post(); ?>
Explanation:
$query_string: Contains current query parameters.&cat=-12: Classic WordPress syntax to exclude category ID 12.
Important Notes & Best Practice
1. Find Category ID: In WordPress admin, go to Posts → Categories, hover over a category, and check the link in your browser's status bar for tag_ID=12 (12 is the ID).
2. query_posts Warning: While used here, query_posts is not officially recommended as it overrides the main query. The modern approach is the pre_get_posts hook.
3. Best Practice: pre_get_posts Hook (Recommended): Add this to your theme's functions.php:
<?php
function exclude_category_from_home($query) {
if ($query->is_home() && $query->is_main_query()) {
$query->set('category__not_in', array(12));
}
}
add_action('pre_get_posts', 'exclude_category_from_home');
?>
Using pre_get_posts is safer, more maintainable, and doesn't require template changes. It's the preferred method for new projects.