12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Feb 26, 2021

Get Child and Grandchild Pages in WordPress

Sridhar Katakam

This tutorial provides code snippets for pulling all descendants of the current Page including grandchild Pages in WordPress.

Given the above Pages structure, when Page A is being viewed:

Unordered List

<ul>
    <?php
    wp_list_pages( array(
        'title_li'    => '',
        'child_of'    => get_the_ID(),
    ) );
    ?>
</ul>

IDs Array – Method 1

<?php

$child_pages = get_pages(
  array(
	'child_of' => get_the_ID(),
	)
);

$child_pages_ids = wp_list_pluck( $child_pages, 'ID' );

echo '<pre>' . print_r( $child_pages_ids, true ) . '</pre>';

?>

IDs Array – Method 2

<?php

$child_pages_objects = get_page_children( get_the_ID(), get_pages() );

$child_pages_ids = wp_list_pluck( $child_pages_objects, 'ID' );

echo '<pre>' . print_r( $child_pages_ids, true ) . '</pre>';

?>

References

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right