Displaying child page content of a certain parent

This is a near duplicate of a number of questions here, but the “single random result” might make it somewhat distinct.

$args = array(
    'post_type'         => 'page',
    'post_parent'       => $post->ID,                               
    'orderby'           => 'rand',
    'posts_per_page'    => 1,
    'no_found_rows'     => true
);
$child = new WP_Query($args);
var_dump($child->posts);

You are telling WP_Query to pick 1) pages, 2) with the parent of the current page, 3) ordered randomly, 4) one result, and 5) don’t bother tracking found rows since we already know it should be 0 or 1.

There are numerous examples of creating multiple loops here and in the Codex. You should be able to find plenty of information to flesh this out. In short you want:

while ($child->have_posts()) {
  $child->the_post();
  var_dump($post); // raw dump of your child post
}

Leave a Comment