bbp_get_all_child_ids( int $parent_id, string $post_type = 'post' )

Query the DB and get the child id’s of all children


Description Description


Parameters Parameters

$parent_id

(Required) Parent id

$post_type

(Optional) Post type. Defaults to 'post'

Default value: 'post'


Top ↑

Return Return

(array) The array of children


Top ↑

Source Source

File: includes/common/functions.php

function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) {

	// Bail if nothing passed
	if ( empty( $parent_id ) || empty( $post_type ) ) {
		return array();
	}

	// Make cache key
	$not_in = array( 'draft', 'future' );
	$key    = md5( serialize( array(
		'parent_id'   => $parent_id,
		'post_type'   => $post_type,
		'post_status' => $not_in
	) ) );

	// Check last changed
	$last_changed = wp_cache_get_last_changed( 'bbpress_posts' );
	$cache_key    = "bbp_child_ids:{$key}:{$last_changed}";

	// Check for cache and set if needed
	$child_ids = wp_cache_get( $cache_key, 'bbpress_posts' );

	// Not already cached
	if ( false === $child_ids ) {

		// Join post statuses to specifically exclude together
		$post_status = "'" . implode( "', '", $not_in ) . "'";
		$bbp_db      = bbp_db();

		// Note that we can't use WP_Query here thanks to post_status assumptions
		$query       = $bbp_db->prepare( "SELECT ID FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status NOT IN ( {$post_status} ) AND post_type = %s ORDER BY ID DESC", $parent_id, $post_type );
		$child_ids   = (array) $bbp_db->get_col( $query );

		// Always cache the results
		wp_cache_set( $cache_key, $child_ids, 'bbpress_posts' );
	}

	// Make sure results are INTs
	$child_ids = wp_parse_id_list( $child_ids );

	// Filter & return
	return (array) apply_filters( 'bbp_get_all_child_ids', $child_ids, $parent_id, $post_type );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.