How to show only certain categories on the Posts Page

11
10
3.5K followers

So I have recipes and articles as categories for my posts. On my Posts Page it shows all my posts but I don't want to show the recipes because I have a page that links to those. So how do I keep the recipe posts from showing up on my Posts Page?

** Before making any changes to your theme files make sure you create a child theme so updates to your theme won't wipe out all your changes! **

First thing we want to do is see what the ID numbers are for the categories you don't want to show.

In WP admin

Go to Posts/Categories

Hover your mouse over the name of the category - with your mouse hovering the name look down at your address bar at the bottom you will see in the link 'category&tag_ID=' and then the ID number of the category.

Write down the category ID's for all the categories you don't want to show in your Posts Page.


Now to edit the code to exclude those categories.

In WP admin

Go to Appearance/Editor

On the right side click on "Main Index Template" it has (index.php) under it.

Find this code just a little ways down the page:

<?php if ( have_posts() ) : ?>

<?php /* Start the Loop */ ?>

<?php while ( have_posts() ) : the_post(); ?>


Just before the top line - <?php if ( have_posts() ) : ?> paste the following line of code:

<?php $query = new WP_Query( 'cat=-4' ); ?>

This line of code excludes my recipe category ID number 4. If I wanted to exclude 2 categories I would add a comma and a negative sign (-) before my next category ID number.

Example:

<?php $query = new WP_Query( 'cat=-4, -5' ); ?>


Now change the last line - <?php while ( have_posts() ) : the_post(); ?> to include $query-> just before have_posts and the_post:

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

I also put in a comment above everything I change so I have a note. Notes can be put in between these tags <!-- and -->.

Here is what my code looks like when finished. Feel free to copy and just replace your category id's.

<!--Only shows 'article' category ID=4 in Posts Page-->

<?php $query = new WP_Query( 'cat=-4' ); ?>

<?php if ( $query->have_posts() ) : ?>

<?php /* Start the Loop */ ?>

<?php while ( $query->have_posts() ) : $query->the_post(); ?>


Click 'Update File' and you should be good to go!


I hope this helps! :)

11
10

Join FREE & Launch Your Business!

Exclusive Bonus - Offer Ends at Midnight Today

00

Hours

:

00

Minutes

:

00

Seconds

2,000 AI Credits Worth $10 USD

Build a Logo + Website That Attracts Customers

400 Credits

Discover Hot Niches with AI Market Research

100 Credits

Create SEO Content That Ranks & Converts

800 Credits

Find Affiliate Offers Up to $500/Sale

10 Credits

Access a Community of 2.9M+ Members

By continuing, you agree to our Terms of Service and Privacy Policy
No credit card required

Recent Comments

10

love your website :)

1

I have a feeling that there are plugins to help with this. There is certainly one to show ids and another to add categories and tags to pages.

1

If there is I certainly couldn't find it. I went through about a dozen different plugins that were supposed to work but they didn't. I believe that I have a template that overrides all the changes that those plugins were supposed to do.

This was the only solution that I could find.

1

I will try to dig out the info. I am on my IPad at the moment so inserting links is a bit difficult

1

This may come in really handy. Thanks.

1

Thanks for great info! It could be helpful. I marked your post as Important.

1

Thanks for sharing the info.

1

Thanks for your info I will keep this for sure

1

Great information and I have saved it for the future!
Sheila

1

Thank you for sharing Charley.

1

See more comments

Join FREE & Launch Your Business!

Exclusive Bonus - Offer Ends at Midnight Today

00

Hours

:

00

Minutes

:

00

Seconds

2,000 AI Credits Worth $10 USD

Build a Logo + Website That Attracts Customers

400 Credits

Discover Hot Niches with AI Market Research

100 Credits

Create SEO Content That Ranks & Converts

800 Credits

Find Affiliate Offers Up to $500/Sale

10 Credits

Access a Community of 2.9M+ Members

By continuing, you agree to our Terms of Service and Privacy Policy
No credit card required

2.9M+

Members

190+

Countries Served

20+

Years Online

50K+

Success Stories

The world's most successful affiliate marketing training platform. Join 2.9M+ entrepreneurs building their online business with expert training, tools, and support.

© 2005-2025 Wealthy Affiliate
All rights reserved worldwide.

🔒 Trusted by Millions Worldwide

Since 2005, Wealthy Affiliate has been the go-to platform for entrepreneurs looking to build successful online businesses. With industry-leading security, 99.9% uptime, and a proven track record of success, you're in safe hands.