bbp_get_public_child_last_id

Query the DB and get the last public post_id that has parent_id as post_parent.

Description

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

Returns (int)

The last active post_id

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_public_child_last_id' ) ) { 
  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_public_child_last_id($parent_id, $post_type); 
  12.  

Defined (1)

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

/includes/common/functions.php  
  1. function bbp_get_public_child_last_id( $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_' . $parent_id . '_type_' . $post_type . '_child_last_id'; 
  10.  
  11. // Check for cache and set if needed 
  12. $child_id = wp_cache_get( $cache_id, 'bbpress_posts' ); 
  13. if ( false === $child_id ) { 
  14. $post_status = array( bbp_get_public_status_id() ); 
  15.  
  16. // Add closed status if topic post type 
  17. if ( $post_type === bbp_get_topic_post_type() ) { 
  18. $post_status[] = bbp_get_closed_status_id(); 
  19.  
  20. // Join post statuses together 
  21. $post_status = "'" . implode( "', '", $post_status ) . "'"; 
  22.  
  23. $child_id = $wpdb->get_var( $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 LIMIT 1;", $parent_id, $post_type ) ); 
  24. wp_cache_set( $cache_id, $child_id, 'bbpress_posts' ); 
  25.  
  26. // Filter and return 
  27. return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, $parent_id, $post_type );