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

Need help implementing a tutorial in your site or want to hire me for custom work?

HIRE ME

Find the article helpful and wish to donate?

DONATE

Comments

You need to be logged in to view and post comments.
Not a member yet? Join here.
magnifiercross
>
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
%d bloggers like this: