Skip to content
TechsBucket

TechsBucket

All About Technology

subscribe techsbucket (1)
Primary Menu
  • Home
  • Linux
  • Mobile
  • Codes
  • Blog
  • Jobs
  • Tools
    • No Copyright Images
    • Text to Audio Converter
    • Resume Generator
    • Image Converter
  • Games
  • Course
  • iPhone
  • Home
  • Codes
  • How to Hide a Post / Category From Home Page in WordPress
  • Codes

How to Hide a Post / Category From Home Page in WordPress

TechsBucket June 10, 2024

WordPress is one of the most versatile and powerful content management systems available today. It allows users to customize their websites to fit their unique needs, including how posts are displayed. One common customization that many users seek is the ability to exclude posts from specific categories on the home page. This can be particularly useful for keeping the front page clean and relevant to your main content focus.

In this comprehensive guide, we will walk you through the process of excluding category posts from the home page of your WordPress site.

Why Exclude Categories from the Home Page?

There are several reasons why you might want to exclude certain categories from your home page:

  1. Focus on Primary Content: Keeping your home page focused on the most important content ensures that visitors see the most relevant posts first.
  2. Organizational Clarity: Excluding certain categories can help in organizing content better, making your site more user-friendly.
  3. Improved Load Time: By limiting the number of posts on the home page, you can improve load times, enhancing the user experience.
  4. Seasonal or Event-Driven Content: Temporarily excluding categories can be useful for seasonal promotions or events that you do not want to feature prominently all year round.

Exclude Categories from the Home Page

Step 1: Identify the Category ID

The first step in excluding a category is to identify the category ID. Each category in WordPress has a unique ID number.

  1. Navigate to the WordPress dashboard.
  2. Go to Posts > Categories.
  3. Hover over the category name you wish to exclude.
  4. Look at the URL in your browser’s status bar. It will look something like this: edit-tags.php?action=edit&taxonomy=category&tag_ID=3. The number after tag_ID= is your category ID.

Step 2: Add Code to functions.php

Next, you need to add a small piece of code to your theme’s functions.php file. This file controls many aspects of how your theme behaves.

  1. Go to Appearance > Theme Editor in the WordPress dashboard.
  2. Select the functions.php file from the list on the right.
  3. Add the following code to the file:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function exclude_category_on_home($query) {
if ($query->is_home() && $query->is_main_query()) {
$query->set('cat', '-YOUR_CATEGORY_ID');
}
}
add_action('pre_get_posts', 'exclude_category_on_home');
function exclude_category_on_home($query) { if ($query->is_home() && $query->is_main_query()) { $query->set('cat', '-YOUR_CATEGORY_ID'); } } add_action('pre_get_posts', 'exclude_category_on_home');
function exclude_category_on_home($query) {
    if ($query->is_home() && $query->is_main_query()) {
        $query->set('cat', '-YOUR_CATEGORY_ID');
    }
}
add_action('pre_get_posts', 'exclude_category_on_home');

Replace YOUR_CATEGORY_ID with the actual ID of the category you want to exclude. If you want to exclude multiple categories, you can list them separated by commas, like this: ‘-1,-2,-3’.

Step 3: Save Changes

After adding the code, save the changes to the functions.php file. This will immediately take effect, and the specified categories will be excluded from the home page.

Advanced Customization Options

If you want more control over how categories are excluded, there are several advanced techniques you can use.

Excluding Categories from RSS Feeds

You may also want to exclude certain categories from appearing in your site’s RSS feed. To do this, add the following code to your functions.php file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function exclude_category_from_rss($query) {
if ($query->is_feed() && $query->is_main_query()) {
$query->set('cat', '-YOUR_CATEGORY_ID');
}
}
add_action('pre_get_posts', 'exclude_category_from_rss');
function exclude_category_from_rss($query) { if ($query->is_feed() && $query->is_main_query()) { $query->set('cat', '-YOUR_CATEGORY_ID'); } } add_action('pre_get_posts', 'exclude_category_from_rss');
function exclude_category_from_rss($query) {
    if ($query->is_feed() && $query->is_main_query()) {
        $query->set('cat', '-YOUR_CATEGORY_ID');
    }
}
add_action('pre_get_posts', 'exclude_category_from_rss');

Excluding Categories from Specific Pages

If you want to exclude categories from specific pages but not others, you can modify the code to check for additional conditions. For example, to exclude a category from both the home page and the archive pages:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function exclude_category_on_home_and_archive($query) {
if (($query->is_home() || $query->is_archive()) && $query->is_main_query()) {
$query->set('cat', '-YOUR_CATEGORY_ID');
}
}
add_action('pre_get_posts', 'exclude_category_on_home_and_archive');
function exclude_category_on_home_and_archive($query) { if (($query->is_home() || $query->is_archive()) && $query->is_main_query()) { $query->set('cat', '-YOUR_CATEGORY_ID'); } } add_action('pre_get_posts', 'exclude_category_on_home_and_archive');
function exclude_category_on_home_and_archive($query) {
    if (($query->is_home() || $query->is_archive()) && $query->is_main_query()) {
        $query->set('cat', '-YOUR_CATEGORY_ID');
    }
}
add_action('pre_get_posts', 'exclude_category_on_home_and_archive');

Video Tutorial

Common Issues and Troubleshooting

Code Not Working

If the code does not seem to work, check the following:

  • Ensure you have correctly identified the category ID.
  • Make sure the code is added to the functions.php file of the active theme.
  • Verify there are no syntax errors in the code.

Conflicts with Other Plugins

Sometimes, other plugins may interfere with custom queries. Deactivate other plugins temporarily to see if the issue resolves.

Theme Updates

Remember that updates to your theme may overwrite changes made to the functions.php file. To avoid losing your custom code, consider creating a child theme and adding the code there.

Conclusion

Excluding specific category posts from the home page of your WordPress site is a powerful way to maintain control over your content presentation. By following the steps outlined in this guide, you can ensure your home page remains focused and relevant to your audience. Whether you’re looking to streamline your site’s appearance, improve user experience, or simply manage content more effectively, these techniques offer a robust solution.

A big thank you for exploring TechsBucket! Your visit means a lot to us, and we’re grateful for your time on our platform. If you have any feedback or suggestions, we’d love to hear them. Looking forward to serving you again soon!

Continue Reading

Previous: All India Study NDLI Android Application Download
Next: iOS 18 takes iPhone to greater levels of customization, productivity, and smarts

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related

Personal Portfolio Website Builder with Full Code Examples
  • Codes

Personal Portfolio Website Builder with Full Code Examples

TechsBucket January 29, 2025
How to Develop a Budget Tracker Tool
  • Codes

How to Develop a Budget Tracker Tool

TechsBucket January 4, 2025
How to Build a To-Do List Application
  • Codes

How to Build a To-Do List Application

TechsBucket January 4, 2025

Popular

Snapdragon Features and Performance Review Snapdragon Features and Performance Review

Snapdragon Features and Performance Review

May 4, 2025
Google Pixel 9a Review: The Best Mid-Range Phone of 2025 Google Pixel 9a Review: The Best Mid-Range Phone of 2025

Google Pixel 9a Review: The Best Mid-Range Phone of 2025

May 3, 2025
Realme P3x 5G Gets a Massive Price Drop Realme P3x 5G Gets a Massive Price Drop

Realme P3x 5G Gets a Massive Price Drop

May 3, 2025
Linux Server Hardening: 10 Must-Do Tasks Linux Server Hardening: 10 Must-Do Tasks

Linux Server Hardening: 10 Must-Do Tasks

April 30, 2025
Steps to Set Up a Linux Web Server Steps to Set Up a Linux Web Server

Steps to Set Up a Linux Web Server

April 30, 2025
Samsung Galaxy S25 Edge Samsung Galaxy S25 Edge

Samsung Galaxy S25 Edge

April 28, 2025
AdSense Responsive Ads
  • Home
  • About
  • Contact
  • DMCA
  • Privacy Policy
  • YouTube
  • Facebook
TechsBucket © All Rights Reserved | MoreNews by AF themes.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Accept

Ad-Block Detected :((

Sorry, We detected that you have activated Ad-Blocker.
Please consider supporting us by disabling your Ad-Blocker. It helps us in maintaining this website.
To view the content, disable the ad-blocker and refresh the page.

Thank You !!!