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?


1 Answer 1


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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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