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 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?

share|improve this question

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 {  }
share|improve this answer
Please accept your answer (using the green tick) – shea Dec 13 '12 at 3:19
And get a badge in the process as well! – kidakaka Dec 13 '12 at 5:37

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.