If you have spent 6 months or so playing with wordpress and now you think it’s timeimage to optimize it to meet your needs then you probably would have though of controlling which posts to show on the main page and which ones to omit for a variety of reasons.

The reason for writing this small tip is because I did not find any satisfactory or even working solution when I needed it. The Category Visibility Plugin and several other don’t support the latest wordpress releases. They were made for WP 2.0.0.X and are obsolete now.

We were redesigning ProgrammerFish and wanted to include another category and the requirement was:

It should act as a separate section of the blog which implies that it’s posts should not be included on the main page.

So this means hiding posts of a specific category or categories from the main page. This is how it is done:

1)

Create/select the category whose posts will be hidden from the main page. For example, I have selected tips and tricks category. Now note down the CATEGORY_ID. My tips and tricks category id is 179

2)

Now open index.php (from WordPress admin page’s sidebar selectAppearance –> Editor –> Main Index Template (index.php) )and paste this code:

 if (is_home()) {
query_posts('cat=-ENTER YOUR CATEGORY ID HERE');

}
else{
wp_reset_query();
}

image

What this code does is check if the current page is index page and if yes then it simply subtracts posts of the category specified from the main page. Remember there’s a minus sign before the category ID, don’t forget to put that.

Note: In case of multiple entries use this code:

 if (is_home()) {
query_posts('cat=-179 , –150 , IT GOES ON AND ON…');

}
else{
wp_reset_query();
}

Once done with this, insert this code at the end of index.php (inside the php tags of course):

image

This code resets the changes we made to the query (by omitting certain categories)

3)

That’s it! Now my Tips and Tricks category posts will not be shown on the main page rather they’ll be shown on the category page i.e.

https://www.programmerfish.com/category/tips-and-tricks