How to hide a category posts from the wordpress homepage

This is a simple tutorial to hide all posts in a particular category from the wordpress homepage. This could be useful when you want to avoid your premium or paid reviews or posts coming in the homepage confusing your regular readers, still giving the reviewed product or website maximum exposure by adding them to your blog and thereby to the search engine.

  1. Firstly, find the category id that you want to exclude: how to find the category id
  2. Open the theme editor from: wordpress admin -> appearances -> editor -> functions.php
  3. At the end of the file before “ ?> ” at the end of the php code paste the below new function

function exclude_category($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-1327’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘exclude_category’);

Please note that the number 1327 is my category to exclude, so if yours is 1234 it would look like below. (only replace the number)

function exclude_category($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-1234’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘exclude_category’);

Further more

If you want to exclude from archives replace the second line with

if ( $query->is_archive ) {

If you want to exclude multiple use

if ( $query->is_home || $query->is_archive) {

Hope the small tip, helped you.

4 Comments

  1. Thomas February 13, 2011
    • Joshu February 15, 2011
  2. Mahesh May 20, 2011
    • Joshu May 21, 2011