How to remove post categories from the homepage

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:

/**
 *
 * Remove post categories from the homepage
 */
if ( ! function_exists( 'ns_exclude_category_home' ) ) {
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 feel 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 contacting me (here)

ABOUT AUTHOR

Nuno

I am a Freelance Web Developer and WordPress Expert based in London with a wealth of website development and support experience. I am great at problem solving and developing quick solutions.

  • LET'S TALK!

    Fill in the form below to make an enquiry or find my contact details on my contact page.

  • This form collects your name and email address so that I can respond to the enquiry(ies) that you submit via this website. Please check my privacy policy for the full story on how I use, protect and manage your submitted data.