Opened 12 years ago
Closed 12 years ago
#2195 closed defect (bug) (fixed)
Topic missing on single topic page when query includes 'posts__not_in'
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 2.3 | Priority: | normal |
Severity: | normal | Version: | 2.2.3 |
Component: | Component - Topics | Keywords: | |
Cc: |
Description
Steps to reproduce:
- Set up forum, with topic and replies (viewing a topic includes the original topic post, and all the replies)
- 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 ) ); }
- 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()
(http://bbpress.trac.wordpress.org/browser/tags/2.2.4/includes/replies/functions.php#L1422), 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.
- 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 );
- Use
preg_replace
, to allow for some unspecified string in between'WHERE 1=1'
and'AND {$table_name}.post_parent'
?
(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.