WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the followind code:

<?php global $post;
            $categories = get_the_category(); //get all categories for this post
                foreach($categories as $category) {
            $args = array(
                'orderby' => 'date',
                'order' => 'DESC',
                'post_type' => 'post',
                'numberposts' => 10,
                'post__not_in' => array($post->ID),
                'category__in' => $cat_ID
            $cat_posts = get_posts($args);
                if ($cat_posts) {

                    echo 'SOME HTML';

                    foreach ($cat_posts as $cat_post) { ?>

It currently lists all post which contain the same category as the current post you are on. What I would like it to do is to exclude the parent category.

The a example category list / example would look like this:

- Animation
- Film
- Misc

When on a post eith the cat Animation all the other posts with the same cat show up (which is good ) but so does the main parent 'Work'. How do I exclude it?


share|improve this question
up vote 0 down vote accepted

Top level categories will have value 0 for the parent, so check that before adding to the array:

foreach( $categories as $category ) {
    if( 0 != $category->parent ) {
        array_push( $cat_ID, $category->cat_ID );
share|improve this answer
Excellent, thank you! – greydog Jun 22 '13 at 16:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.