0

I am looking to get immediate children of a page ID. I tried below code, but it does not seem to work

$pageIDchild = get_pages(
    array (
        'parent'  => 0, 
        'child_of' =>$pageID,
        'sort_column' => 'menu_order'
    )
);

Any help is greatly appreciated. Thanks!

1 Answer 1

0

As per the get_pages() documentation:

  • 'child_of'
    (int) Page ID to return child and grandchild pages of.

  • 'parent'
    (int) Page ID to return direct children of. Default -1, or no restriction.

So to get immediate children of a specific page, you would use the parent arg and not child_of.

$pages = get_pages( array(
    'parent'      => $pageID,
    'sort_column' => 'menu_order',
) );

// Get the ID of the first child page.
$pageIDchild = ( ! empty( $pages ) )
    ? $pages[0]->ID : 0;

// Or to get all the IDs, you can do:
$all_ids = ( ! empty( $pages ) )
    ? wp_list_pluck( $pages, 'ID' ) : array();
1
  • 1
    Perfect, thanks a lot !
    – Rohit
    Aug 24, 2020 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.