Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to get all sub-posts of a specific (root) parent id.

get_posts( array( 'numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'microsite', 'post_parent' => $root_parent_id, 'suppress_filters' => false ) );

WP-Codex: get_post() function has the post_parent but no child_of parameter.

The advantage of the function get_pages() in combination with the child_of paramenter is "... Note that the child_of parameter will also fetch "grandchildren" of the given ID, not just direct descendants."*

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You will need to loop over those posts and then do more queries for each post, repeating until you find no posts in a query.

e.g.

function get_posts_children($parent_id){
    $children = array();
    // grab the posts children
    $posts = get_posts( array( 'numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'microsite', 'post_parent' => $parent_id, 'suppress_filters' => false ));
    // now grab the grand children
    foreach( $posts as $child ){
        // recursion!! hurrah
        $gchildren = get_posts_children($child->ID);
        // merge the grand children into the children array
        if( !empty($gchildren) ) {
            $children = array_merge($children, $gchildren);
        }
    }
    // merge in the direct descendants we found earlier
    $children = array_merge($children,$posts);
    return $children;
}

// example of using above, lets call it and print out the results
$descendants = get_posts_children($post->ID);
echo '<pre>';
print_r($descendants);
echo '</pre>';

Yes the above function calls itself, it's a recursive function. It will keep calling itself until it reaches down to a point where the post being looked at has no children, then it will return without calling itself, and the whole stack will bubble back up building the array of children. You would do good to do further research in this area.

Note that there is an inherent cost to what you want, regardless of wether you use recursive functions or not, that is tied to how many levels of posts you have. 5 levels of posts will be costlier than 2, and it is not a linear scaling. You may want to use transients to cache your output depending on how you do this.

Another way of reducing the cost is by only looking down the tree of posts a certain number of levels, e.g. grandchildren but no great grandchildren. This can be done by passing in a depth parameter, and decrementing it on each recursive call, making sure to return an empty array at the start if the depth is 0 or lower. Many tutorials on recursive functions use this as an example.

share|improve this answer
1  
Thank you very much! I was doing the same thing but with $wpdb->get_var() :-) –  Paul Geisler Jan 15 '13 at 13:16
add comment

Simply use get_page_children(). It works for every post type (not only pages) and is basically what @TomJNowell showed in the other question, but already implemented by core.

$children = get_page_children( $post->ID, $GLOBALS['wp_query'] );

Above sample is like in Codex. That's why you can simply take the global query object (or any other query object) to be used as search base.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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