How to get a list of all recently published child pages?

Can you please help me on how can I display a list <li> of newly published child pages.
I’ve been looking on google how to use display it using the wp_list_pages(); but can’t find out there. I want to display it like in the “Latest Trainings” or something like that.

3 Answers
3

This will get you the post data for all child pages:

$args = array(
    'post_type'         => 'page',
    'post_parent__not_in'       => array(0),                               
    'no_found_rows'     => true,
);
$child = new WP_Query($args);
var_dump(wp_list_pluck($child->posts,'post_title')); // debugging only

Then pass the IDs to wp_list_pages():

$args = array(
    'post_type'         => 'page',
    'post_parent__not_in'       => array(0),                               
    'no_found_rows'     => true,
);
$child = new WP_Query($args);
$ids = wp_list_pluck($child->posts,'ID');
$ids = implode($ids,',');
$args = array(
    'include'      => $ids,
);
wp_list_pages($args);

Or, even cleaner:

$args = array(
    'post_type'         => 'page',
    'post_parent__not_in'       => array(0),                               
    'no_found_rows'     => true,
    'fields' => 'ids'
);
$child = new WP_Query($args);
$args = array(
    'include'      => $child->posts,
);
wp_list_pages($args);

Please note that there is no error checking to that code.

I’ll see if I can spot a clean way to do this with filters. Check back later.

Leave a Comment