bbp_get_public_child_last_id( int $parent_id, string $post_type = 'post' )
Query the DB and get the last public post_id that has parent_id as post_parent
Description
Parameters
- $parent_id
-
(Required) Parent id
- $post_type
-
(Optional) Post type. Defaults to 'post'
Default value: 'post'
Return
(int) The last active post_id
Source
File: bp-forums/common/functions.php
function bbp_get_public_child_last_id( $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_' . $parent_id . '_type_' . $post_type . '_child_last_id';
// Check for cache and set if needed
$child_id = wp_cache_get( $cache_id, 'bbpress_posts' );
if ( false === $child_id ) {
$post_status = array( bbp_get_public_status_id() );
// Add closed status if topic post type
if ( $post_type === bbp_get_topic_post_type() ) {
$post_status[] = bbp_get_closed_status_id();
}
// Join post statuses together
$post_status = "'" . implode( "', '", $post_status ) . "'";
$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 ) );
wp_cache_set( $cache_id, $child_id, 'bbpress_posts' );
}
// Filter and return
return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, $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.