Exclude a specific category post from the front page

“Post Exclude” is a feature or functionality in WordPress that allows you to exclude specific posts or pages from being displayed in certain contexts. This can be useful in variousscenarios, such as when you want to prevent certain content from appearing in a particular listing or archive.

There are several ways to implement post exclusion in WordPress:

Method – 1 (without plugin)

To exclude a specific category from the front page in WordPress, you can modify the main query of the front page using a custom function in your theme’s functions.php file or in a custom plugin. Here’s how you can achieve this:

function exclude_category_from_front_page( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $excluded_category_id = 5; // Replace 5 with the ID of the category you want to exclude
        $query->set( 'cat', '-' . $excluded_category_id );
    }
}
add_action( 'pre_get_posts', 'exclude_category_from_front_page' );

Replace 5 in $excluded_category_id with the ID of the category you want to exclude. You can find the ID of a category by navigating to the WordPress dashboard, then to Posts > Categories, hover over the category you want to exclude, and look at the URL. The ID will be visible in the URL as tag_ID.

This function hooks into the pre_get_posts action, which allows you to modify the parameters of the main query before it is executed. It checks if the current query is for the home page and is the main query, and then modifies the query to exclude the specified category.

After adding this code to your theme’s functions.php file or a custom plugin, the specified category should no longer appear on the front page.

How i category ID ?

Locate the Category: Look for the category whose ID you want to find. If you hover over the category name or copy the edit link, you’ll see a link at the bottom of your browser window. The link will look something like this: https://yourdomain.com/wp-admin/term.php?taxonomy=category&tag_ID=2&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory
. In this link, the tag_ID=2 part indicates the ID of the category. In this example, the category ID is 2.

Check the image

Method 2 (with plugin):

The “Ultimate Category Excluder” (UCE) WordPress plugin is a tool designed to provide users with control over which categories of posts are displayed in various sections of their WordPress website. It offers a straightforward interface within the WordPress admin dashboard for managing category exclusions.Install the Ultimate Category Excluder from here .UCE allows users to exclude specific categories of posts from being displayed on different parts of their website, including the homepage, archives, feeds, search results, and widgets.

How to install a plugin

After install check the Category name

Overall, the Ultimate Category Excluder plugin is a valuable tool for WordPress users who need to manage category exclusions on their websites. Whether you want to filter out certain types of content from specific sections of your site or customize the display of categories in various contexts, UCE provides a convenient solution with its user-friendly interface and flexible configuration options

One thought on “Exclude a specific category post from the front page

  1. Hairstyles

    Hey just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Chrome. I’m not sure if this is a format issue or something to do with internet browser compatibility but I figured I’d post to let you know. The design look great though! Hope you get the problem resolved soon. Many thanks

    Reply

Leave a Reply

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