Trying to list out child pages with WP_Query

I tested the query and it does work so the only thing I can spot that is misleading but not right is directly after you while() statement you have the_post(). This doesn’t work in secondary queries, it should look like:

<?php if( $subpages->have_posts() ) : ?>

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

        ...

    <?php endwhile; ?>

<?php endif; ?>

Note the $subpages->the_post(); – since it’s a secondary query we need to continue referencing the query variable. After this all the normal loop functions work as expected, such as: the_title(), the_content(), etc.

Leave a Comment