Learning never exhausts the mind
Home >  Web Design > WordPress > Prevent WordPress Showing Posts of Sub Categories

Published 20th April 2009 by

In some types of website, you may wish to display ONLY the posts of a category and not posts of its child categories on a Wordpress blog.

Let's say you had a parent category of "Weather" with sub-categories "America" and "United Kingdom". When a user clicks on the weather category, WordPress will automatically display posts from the Weather category AS WELL AS the posts of America and the United Kingdom.

If this is not the behaviour you are after, there is a template tag trick that you can use in your theme to show posts of the current category ONLY.

If you have an archive.php or category.php open that, otherwise open index.php. Don't forget to back them up just in case things go wrong!

Find the line similar to (start of the loop):

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

and add after it on the next line:

<? if ((is_category()) &amp;&amp; in_category($wp_query->get_queried_object_id())) { ?>

This line checks if we are viewing a category and that the post being processed is a member of the category being viewed. If it is then we will show the post.

$wp_query->get_queried_object_id() returns an integer ID of the category being viewed.

in_cagegory() tests if the current post in the loop is a member of the specified category, in this case, we pass it the current category.

Next need to find the line:

<?php endwhile; ?>

Which is the end of the loop, and add a line BEFORE it containing:

<?php } ?>

This small modification will stop posts of child categories from showing when a parent category is selected. Of course, you will need some posts in Weather otherwise you will have a blank page. You can also use the information on the WordPress Codex template tags to create static pages for each category if you wish.

If you only wish to stop sub-category posts from showing on one particular parent category you can specify the required category in the is_category() method. Simply insert the numerical id of the parent category in the brackets, e.g. is_category(10) will stop sub-category posts from showing only if we are viewing the category with ID of 10. All other categories will retain their default setting.

16 thoughts on “Prevent WordPress Showing Posts of Sub Categories
  • 3rd June 2012 at 12:00 am

    Excellent! I tried a lot of plugins for view posts only from parent category without sub, but when one post is in parent and child category, they weren't working. Thank you very much

  • 8th March 2010 at 12:00 am

    hey dear
    the information u provide is not enough to solve my problems..
    pls explaine in detail...

  • 16th December 2009 at 12:00 am

    YES! After 3 hours searching this nails it. (will have to be sure it doesn't paginate though!

    BTW for anyone who gets a fail: In the top code, you must change the & & to just one &

    get_queried_object_id())) { ?>

  • 4th September 2009 at 12:00 am

    2feedme: thank you a lot!!

  • Falco Stellare
    6th April 2009 at 12:00 am

    Nice solution, I really thank you for your tutorial!!!

  • 10th January 2009 at 12:00 am

    there is an issue when used with wp-pagenavi....

    it counts all the posts that return from the have_posts() function
    so it shows a blank page (or more) in pagenavi.

    i found this and its working.


    sorry for my english.

    • 11th December 2010 at 12:00 am

      Thanx it really works, I have been looking for a solution for quire some time...

  • 8th January 2009 at 12:00 am

    thank you. solved all my problems

  • joe
    23rd December 2008 at 12:00 am

    Perfect! only one addition, how to display one of the categories just one? thank you so much.

  • 24th November 2008 at 12:00 am

    Finally.. I got it.. Thank you so much dude.. I was looking for this for about two weeks.


  • Selim Saka
    27th October 2008 at 12:00 am

    I was looking for this for over one month. Thank you. it worked for me.

  • 20th October 2008 at 12:00 am

    Worked perfect for me. I found the code in archive.php -> themes folder.

  • 17th July 2008 at 12:00 am

    Yep, didn&squot;t work for me either...

  • Richard
    7th May 2008 at 12:00 am

    causes a fatal error in category.php in V 2.51 Any ideas??


  • 17th March 2008 at 12:00 am

    I tried this fix but then the first page of every main category was blank. The &quot;next&quot; page would show entries that were inside that main category, but for some reason the first page still showed up without any posts on it. ???

    • 10th April 2010 at 12:00 am

      Works fine on all the sub categories but the main category is blank, any ideas?


Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.