Skip to:

Opened 11 years ago

Closed 11 years ago

#2195 closed defect (bug) (fixed)

Topic missing on single topic page when query includes 'posts__not_in'

Reported by: stephenharris's profile stephenharris Owned by:
Milestone: 2.3 Priority: normal
Severity: normal Version: 2.2.3
Component: Component - Topics Keywords:


Steps to reproduce:

  1. Set up forum, with topic and replies (viewing a topic includes the original topic post, and all the replies)
  2. Add the following code in a plug-in/theme:
add_action( 'pre_get_posts', 'testing_with_post_not_in' );
function testing_with_post_not_in( $query ){
	$query->set( 'post__not_in', array( 1 ) );	
  1. Revisit the topic page, the topic is now missing, and only the replies are listed.

I did a lot of digging and found the reason is due to the function _bbp_has_replies_where() (, specifically it use a str_replace to target a part of the SQL query to change the index and include the topic along with the replies.

However, when using posts_not_in, WordPress inserts the corresponding SQL fragment in between 'WHERE 1=1' and 'AND {$table_name}.post_parent = {$topic_id}'. Thus the target (see this line) being replaced is no longer present in the SQL (as an exact match).

There are a couple of ways round this, but I wanted to see if there was something I hadn't thought / get others' opinions before submitting a patch.

  1. Break up the str_replace into two parts:
$where = str_replace( 'WHERE 1=1', 'FORCE INDEX (PRIMARY, post_parent) WHERE 1=1', $where );

$where = str_replace( 'AND {$table_name}.post_parent = {$topic_id}', 'AND {$table_name}.post_parent = {$topic_id}', $where );
  1. Use preg_replace, to allow for some unspecified string in between 'WHERE 1=1' and 'AND {$table_name}.post_parent'?

Change History (2)

#1 @johnjamesjacoby
11 years ago

  • Component changed from General to Topics
  • Keywords dev-feedback 2nd-opinion removed
  • Milestone changed from Awaiting Review to 2.3

#2 @johnjamesjacoby
11 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [4761]) When using 'postnot_in' or 'postin' query variables in single topic views, do not attempt to force the query index in _bbp_has_replies_where(). Fixes #2195.

Note: See TracTickets for help on using tickets.