I have a custom post type called "films", and most films have a subpage (child page) called "press".

I am trying to loop through the films, and check to see if a subpage exists. If a subpage exists, loop through the content. If it doesn't exist, leave out the content. My loop is as follows:

<?php $loop = new WP_Query( array( 'post_type' => 'films', 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'available-now-shows-on-homepage' ) ); ?>

<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

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

<?php endwhile; ?>

I only want the linked title to show if it's page has children (a press page). How would I modify the loop to do just that?

1 Answer 1


I just figured out the answer to my own question. It works by counting the number of children pages using get_pages. Here is the working code:

$children = get_pages( array('child_of' => $post->ID,'post_type'=>'custom-post-type-name'));
if( count( $children ) != 0 ) { ?> 

<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

<?php }
else {  }
  • 1
    Please accept your answer (using the green tick)
    – shea
    Commented Dec 13, 2012 at 3:19
  • And get a badge in the process as well! Commented Dec 13, 2012 at 5:37

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.