_bbp_has_replies_where( string $where = '', $query = false )
Used by bbp_has_replies() to add the lead topic post to the posts loop
Description Description
This function filters the ‘post_where’ of the WP_Query, and changes the query to include both the topic AND its children in the same loop.
Parameters Parameters
- $where
-
(Optional)
Default value: ''
Return Return
(string)
Source Source
File: includes/replies/functions.php
function _bbp_has_replies_where( $where = '', $query = false ) {
/** Bail ******************************************************************/
// Bail if the sky is falling
if ( empty( $where ) || empty( $query ) ) {
return $where;
}
// Bail if no post_parent to replace
if ( ! is_numeric( $query->get( 'post_parent' ) ) ) {
return $where;
}
// Bail if not a topic and reply query
if ( array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) !== $query->get( 'post_type' ) ) {
return $where;
}
// Bail if including specific post ID's
if ( $query->get( 'post__in' ) ) {
return $where;
}
/** Proceed ***************************************************************/
// Table name for posts
$table_name = bbp_db()->prefix . 'posts';
// Get the topic ID from the post_parent, set in bbp_has_replies()
$topic_id = bbp_get_topic_id( $query->get( 'post_parent' ) );
// The texts to search for
$search = array(
"FROM {$table_name} " ,
"WHERE 1=1 AND {$table_name}.post_parent = {$topic_id}",
") AND {$table_name}.post_parent = {$topic_id}"
);
// The texts to replace them with
$replace = array(
$search[0] . "FORCE INDEX (PRIMARY, post_parent) " ,
"WHERE 1=1 AND ({$table_name}.ID = {$topic_id} OR {$table_name}.post_parent = {$topic_id})",
") AND ({$table_name}.ID = {$topic_id} OR {$table_name}.post_parent = {$topic_id})"
);
// Try to replace the search text with the replacement
$new_where = str_replace( $search, $replace, $where );
if ( ! empty( $new_where ) ) {
$where = $new_where;
}
return $where;
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |