How to show only certain categories on the Posts Page

11
3.5K followers
Updated

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! :)

Login
Create Your Free Wealthy Affiliate Account Today!
icon
4-Steps to Success Class
icon
One Profit Ready Website
icon
Market Research & Analysis Tools
icon
Millionaire Mentorship
icon
Core “Business Start Up” Training

Recent Comments

10

love your website :)

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.

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.

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

This may come in really handy. Thanks.

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

Thanks for sharing the info.

Thanks for your info I will keep this for sure

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

Thank you for sharing Charley.

See more comments

Login
Create Your Free Wealthy Affiliate Account Today!
icon
4-Steps to Success Class
icon
One Profit Ready Website
icon
Market Research & Analysis Tools
icon
Millionaire Mentorship
icon
Core “Business Start Up” Training