Question

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!

Était-ce utile?

La solution

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();
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top