Get first level children of a page ID

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!

Topic children get-children child-pages Wordpress

Category Web


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();

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.