bbp_get_all_child_ids( int $parent_id, string $post_type = 'post' )
Query the DB and get a the child id’s of all children
Description
Parameters
- $parent_id
-
(Required) Parent id
- $post_type
-
(Optional) Post type. Defaults to 'post'
Default value: 'post'
Return
(array) The array of children
Source
File: bp-forums/common/functions.php
function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) {
global $wpdb;
// Bail if nothing passed
if ( empty( $parent_id ) )
return false;
// The ID of the cached query
$cache_id = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';
// Check for cache and set if needed
$child_ids = wp_cache_get( $cache_id, 'bbpress_posts' );
if ( false === $child_ids ) {
// Join post statuses to specifically exclude together
$not_in = array( 'draft', 'future' );
$post_status = "'" . implode( "', '", $not_in ) . "'";
$child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status NOT IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
}
// Filter and return
return apply_filters( 'bbp_get_all_child_ids', $child_ids, (int) $parent_id, $post_type );
}
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.