Skip to content

Instantly share code, notes, and snippets.

@zaerl
Created October 22, 2012 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaerl/3930162 to your computer and use it in GitHub Desktop.
Save zaerl/3930162 to your computer and use it in GitHub Desktop.
bbPress FORCE INDEX workaround
function _bbp_has_replies_where( $where, $query ) {
// 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 meta query
if ( $query->get( 'meta_key' ) || $query->get( 'meta_query' ) )
return $where;
global $wpdb;
// Table name for posts
$table_name = $wpdb->prefix . 'posts';
// Get the topic ID
$topic_id = bbp_get_topic_id();
// The text we're searching for
$search = "WHERE 1=1 AND {$table_name}.post_parent = {$topic_id}";
$index_suff = stripos($where, 'JOIN') === false ? '' : ' FOR JOIN';
// The text to replace it with
$replace = "FORCE INDEX{$index_suff} (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
if ( $new_where = str_replace( $search, $replace, $where ) )
$where = $new_where;
return $where;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment