• Resolved mrarrow

    (@mrarrow)


    Hi

    Can anyone tell me how to return an array of ID’s of root (top level) Pages only.

    So if I’ve got the following structure, I just want to return the ID’s of pages in bold (Test 1, Test 2, Test 3, Test 4, Test 5):

    Test 1
    –child1
    –child2
    –grandchild1
    –grandchild2
    –child3
    Test 2
    –child4
    –child5
    –grandchild1
    Test 3
    Test 4
    –child6
    Test 5

    So, not parents of children/grandchildren, but actually top level pages.

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • esmi

    (@esmi)

    Thread Starter mrarrow

    (@mrarrow)

    Yes I did – but I could only seem to return the parent ID of child/grandchild posts and more specifically of that current page. Rather than just a list of all top level Page ID’s.

    esmi

    (@esmi)

    Have you tried setting parent to zero?

    parent
    (integer) List those pages that have the provided single page only ID as parent. Defaults to -1 (displays all Pages regardless of parent). Note that the ‘hierarchical’ parameter must be set to 0 (false) — which is not default — or else no results will be returned for any page other than the top level pages with no parent (ID=0) and the default all pages (ID=-1). In contrast to the ‘child_of’ parameter, this parameter only returns the direct descendants of the parent, no ‘grandchildren’. You can obviate the ‘hierarchical’ set to 0 requirement by passing a ‘child_of’ parameter set to the same (parent) ID value.

    -1 – default, no parent restriction
    0 – returns all top level pages

    Thread Starter mrarrow

    (@mrarrow)

    Thanks, I was getting myself a bit confused as I needed a clean array of top level page ID’s and I wasn’t getting it until I did a foreach.

    So for others, this is my answer (thanks to Esmi too 🙂

    $pageids = array();
    $arrs=get_pages( array( 'parent' => 0,'post_type' => 'page', 'post_status' => 'publish' ) );
        foreach($arrs as $arr){
         $excludeparents[] = $arr->ID;
    }
    //print_r($excludeparents);

    esmi

    (@esmi)

    Glad to hear that you got this sorted. 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Return an array of ID's’ is closed to new replies.