_bbp_has_replies_where( string $where = '',  $query = false )

Used by bbp_has_replies() to add the lead topic post to the posts loop

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

$where

(string) (Optional)

Default value: ''

Return

(string)

Source

File: bp-forums/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 or excluding specific post ID's
	if ( $query->get( 'post__not_in' ) || $query->get( 'post__in' ) ) {
		return $where;
	}

	/** Proceed ***************************************************************/

	global $wpdb;

	// Table name for posts
	$table_name = $wpdb->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}"
	);

	// 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})"
	);

	// 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
bbPress (r4058) Introduced.

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.