Show Only One Category on Your WordPress Homepage

If you want to show posts from only one category on your homepage, you can do it easily. First find the category ID number of the category you want to show up. You can do this by mousing over the category title (Posts > Categories), and then look in the bottom of your browser. You should see the category ID among a string of other messy info.

Insert Code

Once you have your category ID, put that into the following code, which should go into your functions file. (Appearances > Editor > Theme Functions – functions.php)
As you’ll be changing your theme’s function file, it would be wise to use a child theme.
In the example code below, I’m using category ID number 11. Replace that with your category ID.

function my_home_category( $query ) { if ( $query->is_home()
&& $query->is_main_query() ) { $query->set( 'cat', '11'); } }
add_action( 'pre_get_posts', 'my_home_category' );

If you would like to include more than one category, then just add another ID by inserting a comma and the ID number. For example, this is will display category 11 and category 14.

$query->set( 'cat', ’11, 14’ );

And that’s all there is to it. You should now only see posts on your homepage from the category you singled out in your code.

Leave a Reply

Your email address will not be published. Required fields are marked *