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.
- Firstly, find the category id that you want to exclude: how to find the category id
- Open the theme editor from: wordpress admin -> appearances -> editor -> functions.php
- 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.
I was googling how to exclude categories. I have more or less what you suggested. Any way to append multiple categories instead of just one to the exclusion?
Thomas,
Check the last line of the article, it tells you what to do when you have to exclude multiple categories.
cheers
josh
After adding the above code to my theme's "functions.php" file. I got this error –
"Parse error: syntax error, unexpected T_STRING in /home/username/public_html/wp-content/themes/themename/functions.php on line 77"
Dear Mahesh,
Which theme are you using?
This is basically straight forward method to hide categories form your main navigation and works well for many blogs.
make sure you dont miss out even a bit and replace the original functions.php from the theme and try again.
warm regards
Josh