How to Hide Category posts from front Page


Recently when I was working on some C++ tutorials on recursion and published 30 Best recursive Programs in C++ then suddenly realized that this is not my niche on binarynote. Most of the time these tutorials will contain codes.

This code will be beneficial for beginners trying to learn coding in C/C++ but most of my readers are not programmers they are lazy bloggers like me who want to make money on auto-pilot using scripts or themes.

But before that let’s concentrate on our main problem first-How to hide category posts from front page. Right now I have more than one solution for this issue.

Hide category Posts from front Page using Ultimate Category Excluder plugin

Ultimate Category Excluder, abbreviated as UCE, is a WordPress plugin that allows you to quickly and easily exclude categories from your front page, archives, feeds, and searches. Just select which categories you want to be excluded, and UCE does all the work for you!

Category Excluder Plugin in action

Category Excluder Plugin in action

The above screenshot will help you a lot to understand how much and how you can hide from your website front end.

Hide Category posts from Front Page without Plugin

As you are already aware that installing too much plugins for each and evry small issue is not a good idea as each plugin store its settings and other option values in wp_option database and when any one access any page from your website the whole database specially this wp_option is  queried again and again , Thus it increase the page loading time of your website.

You can also read my post on how to reduce page loading time.  So here we will explore the WordPress hooks to filter our data, so that we can hide category post from front page.

pre_get_post hook execute every time before sending any data to the global $query variable.

But before that you have to find out what is the category ID of your desired category.

To know category ID of your category just hover the mouse on the category name and its ID will be visible at the status bar of the WordPress admin Panel.

WordPress-Category-ID-from-URL-in-WordPress-Dashboard

WordPress Category-ID from URL in WordPressDashboard

Now open your theme’s functions.php file and paste the following code

function exclude_category_frontpage($query){
if(is_home()){
$query->set('cat','-9999'); 
}
return $query;
}
add_action(pre_get_post,'exclude_category_frontpage');

In the above code (-) minus sign is compulsory otherwise above code will not exclude any post from the frontpage. If you want to exclude more than one category then there is no need to pass array, just separate your categories with comma(,) like this

$query->set('cat','-123,-1234,-2345');

The same way if you want to exclude category from other type of pages like archive page, just extend your if statement like this

if(is_home() || is_archive()) {
$query->set('cat','-12345');
}

AND if you want to show post from some categories only then remove (-) minus sign from the code and this will show only the post from that categories only.

function exclude_category_frontpage($query){
if(is_home()){
$query->set('cat','123,1234,12345'); 
}
return $query;
}

What else you want to display except hiding category post from home  page.




One Brilliant Comment - Join Discussion Now!

  1. Sonia Pitt says:

    OK, now this can be helpful for many WordPress Bloggers. Though there are some other plugins also for the same purpose which also do the job perfectly but this one seems to be better. Thanks for sharing this information.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>