Sometimes you don’t want individual categories to be displayed on the blog homepage – in the case of allsky-rodgau.de the “Offtopic” category. WordPress does not offer a direct setting for this, but the behavior can be precisely controlled via functions.php.
Why hide categories?
Especially with smaller blogs or projects, content can quickly end up in a category that should not appear in the main feed. Instead of installing cumbersome workarounds, you can simply instruct WordPress to completely ignore certain category IDs.
This is how it works
The following code ensures that the category IDs XX and XY no longer appear on the blog homepage. The solution is update-safe, only works in the frontend and also takes pagination into account.
/**
* Hide certain categories on the blog homepage
*/
function allsky_exclude_categories_from_blog( $query ) {
// Only on the main page of the blog AND only in the frontend
if ( $query->is_home() && $query->is_main_query() && ! is_admin() ) {
// Categories that are to be excluded
$exclude = array( XX, XY );
// IDs with a minus sign must be excluded in WP_Query
$query->set( 'cat', '-' . implode( ',-', $exclude ) );
}
}
add_action( 'pre_get_posts', 'allsky_exclude_categories_from_blog' );
Result
After inserting the code, posts from the defined categories no longer appear in the blog feed. Individual views and category archives remain unaffected. A simple, efficient solution for more control over the start page of your own blog.