get_children

Retrieve all children of the post parent ID.

Description

(array) get_children( (string) $args = '', (string) $output = ); 

Normally, without any enhancements, the children would apply to pages. In the context of the inner workings of WordPress, pages, posts, and attachments share the same table, so therefore the functionality could apply to any one of them. It is then noted that while this function does not work on posts, it does not mean that it won't work on posts. It is recommended that you know what context you wish to retrieve the children of.

Attachments may also be made the child of a post, so if that is an accurate statement (which needs to be verified), it would then be possible to get all of the attachments for a post. Attachments have since changed since version 2.5, so this is most likely inaccurate, but serves generally as an example of what is possible.

The arguments listed as defaults are for this function and also of the get_posts() function. The arguments are combined with the get_children defaults and are then passed to the get_posts() function, which accepts additional arguments. You can replace the defaults in this function, listed below and the additional arguments listed in the get_posts() function.

The post_parent is the most important argument and important attention needs to be paid to the $args parameter. If you pass either an object or an integer (number), then just the post_parent is grabbed and everything else is lost. If you don't specify any arguments, then it is assumed that you are in The Loop and the post parent will be grabbed for from the current post.

The post_parent argument is the ID to get the children. The numberposts is the amount of posts to retrieve that has a default of -1,, which is used to get all of the posts. Giving a number higher than 0 will only retrieve that amount of posts.

The post_type. and post_status arguments can be used to choose what criteria of posts to retrieve. The post_type. can be anything, but WordPress post types are post, pages, and attachments. The post_status argument will accept any post status within the write administration panels.

Returns (array)

Array of children, where the type of each element is determined by $output parameter. Empty array on failure.

Parameters (2)

0. $args — Optional. (string) => ''
User defined arguments for replacing the defaults. Default empty.
1. $output — Optional. (string)
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT.

Usage

  1. if ( !function_exists( 'get_children' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. User defined arguments for replacing the defaults. Default empty. 
  5. $args = ''; 
  6.  
  7. // Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  8. // a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. 
  9. $output = ''; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = get_children($args, $output); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function get_children( $args = '', $output = OBJECT ) { 
  2. $kids = array(); 
  3. if ( empty( $args ) ) { 
  4. if ( isset( $GLOBALS['post'] ) ) { 
  5. $args = array('post_parent' => (int) $GLOBALS['post']->post_parent ); 
  6. } else { 
  7. return $kids; 
  8. } elseif ( is_object( $args ) ) { 
  9. $args = array('post_parent' => (int) $args->post_parent ); 
  10. } elseif ( is_numeric( $args ) ) { 
  11. $args = array('post_parent' => (int) $args); 
  12.  
  13. $defaults = array( 
  14. 'numberposts' => -1, 'post_type' => 'any',  
  15. 'post_status' => 'any', 'post_parent' => 0,  
  16. ); 
  17.  
  18. $r = wp_parse_args( $args, $defaults ); 
  19.  
  20. $children = get_posts( $r ); 
  21.  
  22. if ( ! $children ) 
  23. return $kids; 
  24.  
  25. if ( ! empty( $r['fields'] ) ) 
  26. return $children; 
  27.  
  28. update_post_cache($children); 
  29.  
  30. foreach ( $children as $key => $child ) 
  31. $kids[$child->ID] = $children[$key]; 
  32.  
  33. if ( $output == OBJECT ) { 
  34. return $kids; 
  35. } elseif ( $output == ARRAY_A ) { 
  36. $weeuns = array(); 
  37. foreach ( (array) $kids as $kid ) { 
  38. $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]); 
  39. return $weeuns; 
  40. } elseif ( $output == ARRAY_N ) { 
  41. $babes = array(); 
  42. foreach ( (array) $kids as $kid ) { 
  43. $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID])); 
  44. return $babes; 
  45. } else { 
  46. return $kids;