Hide a certain Category’s Posts from the Home/Blog page in WordPress

To hide a certain Category’s posts from the home or blog page you just need to know the category ID and then use a filter on the pre_get_posts hook to exclude those posts.

To find the ID for the Category just go to the Category and hover over the edit button and the ID appears in the URL at the bottom.


add_action( 'pre_get_posts', 'themeprefix_exclude_category' );
// Exclude Category Posts from Home Page
function themeprefix_exclude_category( $query ) {
	if ( $query->is_home() ) {
	$query->set( 'cat', '-338' );//add your category number
	return $query;

Then add the function inside your functions.php file. The above function only runs on the home page and will remove any posts that belong in the category numbers listed. In the example above that would be number 338, to exclude more than one category just comma separate all the ID numbers like so; ‘-338, -340, -350’


The reverse of this is also true, if you wanted to only show a particular category then use the same code but without the minus…

add_action( 'pre_get_posts', 'only_portfolio_category' );
// Only Portfolio Category
function only_portfolio_category( $query ) {
   if ( $query->is_home() && $query->is_main_query() ) {
   $query->set( 'cat', '3' );

So in this example, only the posts in a category id of ‘3’ would show.


  1. Naji Khan on April 3, 2020 at 6:34 am

    Is there any WordPress plugin to exclude the specific category and hide from home page?

  2. anne on August 9, 2019 at 7:20 pm

    I have a blog on one page. I want it on two pages. Blog categories (and posts) 1-7, I would like:

    Page X excludes blog categories (and posts) 1, 2, 3.
    Page Y excludes blog categories (and posts) 4, 5, 6, 7.

    I tried using your code and altering it myself. I have no experience and it didnt work. Any chance you can help.

    I found out
    Page ID “post=195”
    Page ID “post=182”

    And also found out all the category IDs

    Thank you for a great post :)

  3. Andy on August 14, 2018 at 2:27 pm

    Thank you!!!! That just saved me a whole bunch of work

  4. Olivia on February 17, 2016 at 1:39 pm


    i have added

    //Exclude Category Posts from Home Page
    function themeprefix_exclude_category($query) {
    if ( $query->is_home() ) {
    $query->set(‘cat’, ‘-4,-3,-5,-6’);//add your category number
    return $query;
    add_action(‘pre_get_posts’, ‘themeprefix_exclude_category’);

    yet the home page is still showing all categories – is there anything you can add to this to help at all?

    many thanks

  5. Carla on October 13, 2015 at 3:27 am

    Thanks for the tutorial.

    I have a category of posts I wanted to completely exclude from displaying in my blog. They are set up to only display on a certain page.

    You said this addresses the homepage, which to me is the page you see when you land on someone’s blog for the first time. But much to my surprise it excludes all of that category from the entire blog, which is exactly what I wanted.

    Could you please help me understand why it works, when only ‘home’ is referenced in the code? Thank you!

  6. Santosh on September 25, 2015 at 5:50 am

    Great! Thanks a lot!

  7. Vicente on September 13, 2015 at 9:14 am

    Hi, this code used to work fine before the latest WP update (4.3), but it now produces the following error:

    Parse error: syntax error, unexpected ‘’’ (T_STRING) in /home/example.com/wp-content/themes/exampletheme/functions.php

    How could I fix it? Thanks!

    • Neil Gee on September 13, 2015 at 11:00 pm

      This sounds more like a PHP syntax error – look for the line number the error gives you and see if you have one extra ‘”‘

  8. Joshua James on May 20, 2015 at 1:35 am

    Thank you. This saved me a bunch of time. Prior to this I didn’t know that I could filter the posts query on the functions level. super powerful trick. I will definitely use this a lot in the future.

Leave all Comment