How to remove post categories from the homepage

 In Functions, Themes

How To Hide or Remove Categories from a WordPress Homepage

When displaying your latest posts on the homepage, you might want to exclude some categories from displaying on this page.

There are many reasons why you would want to do this. You might want to keep your homepage for only news posts and relegate the rest of the posts to other sections accessed through the menu.

In any case, what we will need to modify is the WordPress loop. All WordPress themes make use of the loop, and the best way to modify it is by using the pre_get_posts hook.

Removing categories from the homepage can sometimes turn out to be quite a frustrating act for beginners, so here’s the easy way to do it.

In your theme’s functions.php file, just insert the following code, replacing the category IDs with the ones you want to exclude:

function ns_exclude_category_home( $query ) {

  if ( $query->is_home ) {
    $query->set( 'cat', '-5, -34' );
  }

  return $query;
}
add_filter( 'pre_get_posts', 'ns_exclude_category_home' );

Read more about pre_get_posts here.

Thank you for seeing my code snippet and fell free to share and comment 🙂 .Do you have any interesting changes to your “your latest posts on the homepage” that you make or some other cool code snippet that you’d like to share? Let everyone know in the comments what cool tricks you’ve come up with or you can let me know by contact me (here)

Recommended Posts

Start typing and press Enter to search