Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an array of child posts IDs, wordpress 3.0, php

Ok here's one for ya...

On a custom template I'm using this code to retrieve & display a list of child pages/posts

$args = array(
                    'depth'        => 1,
                    'show_date'    => '',
                    'date_format'  =>     get_option('date_format'),
                    'child_of'     => $post->ID,
                    'exclude'      => '',
                    'include'      => '',
                    'title_li'     => '',
                    'echo'         => 1,
                    'authors'      => '',
                    'sort_column'  => 'menu_order, post_title',
                    'link_before'  => '',
                    'link_after'   => '',
                    'walker' => '' );

                    wp_list_pages( $args );

This works great, I'm also wondering how I can access/create an array of child post ID's. My goal is to access some custom fields meta data through the get_post_meta() function of each child post using it's ID.

Thanks guys.

like image 339
shane Avatar asked Feb 12 '26 11:02

shane


2 Answers

I guess I wasn't very clear with this one as it's the first time I've never recieved an answer from SO.

I managed to find the information I needed and will place it here for anyone else browsing with the same request.

ok - To get all child IDs..

$pages = get_pages('child_of=X');
    foreach($pages as $child) {

    // Now you have an object full of Children ID's that you can use for whatever
    // E.G 
    echo $child->ID . "<br />";
}
like image 66
shane Avatar answered Feb 15 '26 00:02

shane


If you want to build an array of post ids for later use you can do this:

$pageids = array();
$pages = get_pages('child_of=X');
    foreach($pages as $page){
     $pageids[] = $page->ID;
}

And you have a clean array of just page ids.

like image 45
pabloselin Avatar answered Feb 15 '26 02:02

pabloselin