Advertisements

get_children

Definition:
function get_children($args = '', $output = OBJECT) {}

Retrieve all children of the post parent ID.
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.

Parameters

  • mixed $args: Optional. User defined arguments for replacing the defaults.
  • string $output: Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.

Return values

returns:False on failure and the type will be determined by $output parameter.

Source code

function get_children($args = '', $output = OBJECT) {

	$kids = array();

	if ( empty( $args ) ) {

		if ( isset( $GLOBALS['post'] ) ) {

			$args = array('post_parent' => (int) $GLOBALS['post']->post_parent );

		} else {

			return $kids;

		}

	} elseif ( is_object( $args ) ) {

		$args = array('post_parent' => (int) $args->post_parent );

	} elseif ( is_numeric( $args ) ) {

		$args = array('post_parent' => (int) $args);

	}



	$defaults = array(

		'numberposts' => -1, 'post_type' => 'any',

		'post_status' => 'any', 'post_parent' => 0,

	);



	$r = wp_parse_args( $args, $defaults );



	$children = get_posts( $r );



	if ( !$children )

		return $kids;



	update_post_cache($children);



	foreach ( $children as $key => $child )

		$kids[$child->ID] = $children[$key];



	if ( $output == OBJECT ) {

		return $kids;

	} elseif ( $output == ARRAY_A ) {

		foreach ( (array) $kids as $kid )

			$weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);

		return $weeuns;

	} elseif ( $output == ARRAY_N ) {

		foreach ( (array) $kids as $kid )

			$babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));

		return $babes;

	} else {

		return $kids;

	}

}

1268

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: