How to Show Only One Category on Your WordPress Homepage

How to Show Only One Category on Your WordPress Homepage

If you’d like to show posts from only one category on your homepage, then it’s easy enough to do.

We’ll go over the steps below.

featured-one-cat-weekend

Find Your Category ID

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.

In this example, the category is 11.

find-cat-id

You can also use a plugin that will give you different IDs in your Admin area.

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

 

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.

Do you have any other tricks or plugin recommendations to show only one category? Let us know in the comments!

Tags:

Aileen Javier Aileen has over 10 years of experience in content writing and content marketing. She’s handled content teams, planned editorial calendars, and managed projects. She’s also written blogs, web copy, social media posts, and email newsletters for brands in different industries.