WordPress at a glance

get_children() WP 2.0.0

Retrieve all children (attachments, revisions, or sub-pages) of the post parent ID. It works similar to get_posts().

Works based on: get_posts()

No Hooks.

Return

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

Usage

get_children( $args, $output );

Template of usage

$childrens = get_children( array( 
	'post_parent' => 0,
	'post_type'   => 'any', 
	'numberposts' => -1,
	'post_status' => 'any'
) );

if( $childrens ){
	foreach( $childrens as $children ){
		// output
	}
}
$args(mixed)
User defined arguments for replacing the defaults.
Default: ''
$output(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

Parameters of $args

numberposts(int)
Number of child posts to retrieve. -1 — all of them.
Default: -1
post_parent(int)
Pass the ID of a post or Page to get its children. Pass 0 to get attachments without parent. Pass null to get any child regardless of parent.
post_type(string)
Any value from post_type column of the posts table, such as attachment, page, or revision; or the keyword any.
Default: any
post_status(string)
Any value from the post_status column of the wp_posts table, such as publish, draft, or inherit; or the keyword any.
Default: any
post_mime_type(string)
A full or partial mime-type, e.g. image, video, video/mp4, audio which is matched against a post's post_mime_type field.
Default: ''

See get_posts() for a full list of $args parameters.

Examples

#1 ______


#2 ______

Notes

  • See: get_posts()
  • Global. WP_Post. $post

Changelog

Since 2.0.0 Introduced.

Code of get children: wp-includes/post.php WP 5.2.3

<?php
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;
	}

	if ( ! empty( $r['fields'] ) ) {
		return $children;
	}

	update_post_cache( $children );

	foreach ( $children as $key => $child ) {
		$kids[ $child->ID ] = $children[ $key ];
	}

	if ( $output == OBJECT ) {
		return $kids;
	} elseif ( $output == ARRAY_A ) {
		$weeuns = array();
		foreach ( (array) $kids as $kid ) {
			$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
		}
		return $weeuns;
	} elseif ( $output == ARRAY_N ) {
		$babes = array();
		foreach ( (array) $kids as $kid ) {
			$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
		}
		return $babes;
	} else {
		return $kids;
	}
}

Related Functions

From tag: query

More from category: Attachments (Media files)

No comments
    Hello, !     Log In . Register