bbp_get_all_child_ids

Query the DB and get a the child id's of all children.

Description

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

Returns (array)

The array of children

Parameters (2)

0. $parent_id — Optional. (int)
The parent id.
1. $post_type — Optional. (string) => 'post'
Post type. Defaults to post

Usage

  1. if ( !function_exists( 'bbp_get_all_child_ids' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/common/functions.php'; 
  3.  
  4. // The parent id. 
  5. $parent_id = -1; 
  6.  
  7. // Post type. Defaults to 'post' 
  8. $post_type = 'post'; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bbp_get_all_child_ids($parent_id, $post_type); 
  12.  

Defined (1)

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

/includes/common/functions.php  
  1. function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) { 
  2. global $wpdb; 
  3.  
  4. // Bail if nothing passed 
  5. if ( empty( $parent_id ) ) 
  6. return false; 
  7.  
  8. // The ID of the cached query 
  9. $cache_id = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids'; 
  10.  
  11. // Check for cache and set if needed 
  12. $child_ids = wp_cache_get( $cache_id, 'bbpress_posts' ); 
  13. if ( false === $child_ids ) { 
  14. $post_status = array( bbp_get_public_status_id() ); 
  15.  
  16. // Extra post statuses based on post type 
  17. switch ( $post_type ) { 
  18.  
  19. // Forum 
  20. $post_status[] = bbp_get_private_status_id(); 
  21. $post_status[] = bbp_get_hidden_status_id(); 
  22. break; 
  23.  
  24. // Topic 
  25. $post_status[] = bbp_get_closed_status_id(); 
  26. $post_status[] = bbp_get_trash_status_id(); 
  27. $post_status[] = bbp_get_spam_status_id(); 
  28. break; 
  29.  
  30. // Reply 
  31. $post_status[] = bbp_get_trash_status_id(); 
  32. $post_status[] = bbp_get_spam_status_id(); 
  33. break; 
  34.  
  35. // Join post statuses together 
  36. $post_status = "'" . implode( "', '", $post_status ) . "'"; 
  37.  
  38. $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) ); 
  39. wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' ); 
  40.  
  41. // Filter and return 
  42. return apply_filters( 'bbp_get_all_child_ids', $child_ids, (int) $parent_id, $post_type );