In WordPress, we assign different category to set of posts for better overall organization. This helps in better management and easier content discovery. Sometimes we need to exclude the display of specific posts on the blog homepage but show all other posts. This can be easily achieved by assigning same category to all those posts and use following code in functions.php file.

[cc lang=”php”]
//Exclude one category on homepage www.basicwp.com
add_filter(‘pre_get_posts’, ‘exclude_category_from_home’);
function exclude_category_from_home($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-15’);
}
return $query;
}
[/cc]

Above 15 is the ID of the category whose posts wont be displayed on the homepage. You can replace it with any required category ID as on your blog.

To find ID of specific category, open category page from left sidebar in WordPress dashboard. Then mouse over the specific category to view full url at the bottom showing ID at the end.

Leave a Comment