WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#35478 closed defect (bug) (fixed)

4.4 Regression on Querying for Comments by Multiple Post Fields

Reported by: chriscct7 Owned by: boonebgorges
Milestone: 4.4.2 Priority: high
Severity: major Version: 4.4
Component: Comments Keywords: fixed-major
Focuses: Cc:

Description

In 4.4, there has been a regression in using WP_Comment_Query, and querying by multiple post field parameters no longer works.

The cause is this loop that now processes post_fields:

if ( ! empty( $post_fields ) ) {
			$join_posts_table = true;
			foreach ( $post_fields as $field_name => $field_value ) {
				// $field_value may be an array.
				$esses = array_fill( 0, count( (array) $field_value ), '%s' );
				$this->sql_clauses['where']['post_fields'] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
			}
		}

On each iteration of this loop, the previous post_field query is thrown out. Thus a comment query by post_author and post_type will ignore the post_author limiter.

This is caused by [34542] where before each forloop iteration resulted in a new array index. Post [34542] this is not the case, causing the bug.

Additionally the docbloc on post_author is incorrect: Comment author ID to limit results by. Default empty. it should read Post author ID to limit results by. Default empty.

Change History (4)

#1 @chriscct7
2 years ago

  • Milestone changed from Awaiting Review to 4.4.2

I think what can be done is change $this->sql_clauses['where']['post_fields'] to $this->sql_clauses['where'][ $field_name ]

#2 @boonebgorges
2 years ago

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

In 36326:

Respect all post-related filters in WP_Comment_Query.

The refactor of WP_Comment_Query's SQL generation in [34542] introduced a bug
that caused only the last post-related filter to be respected in comment
queries. In other words, if querying for comments using params
post_status=draft&post_author=3, only the last-processed of these params
would be respected. The current changeset fixes the logic so that these clauses
don't overwrite each other.

Props chriscct7.
Fixes #35478.

#3 @boonebgorges
2 years ago

  • Keywords fixed-major added; needs-patch needs-unit-tests removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for 4.4.2.

#4 @dd32
2 years ago

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

In 36361:

Comments: Respect all post-related filters in WP_Comment_Query.

The refactor of WP_Comment_Query's SQL generation in [34542] introduced a bug
that caused only the last post-related filter to be respected in comment
queries. In other words, if querying for comments using params
post_status=draft&post_author=3, only the last-processed of these params
would be respected. The current changeset fixes the logic so that these clauses
don't overwrite each other.

Merges [36326] to the 4.4 branch.
Props chriscct7.
Fixes #35478.

Note: See TracTickets for help on using tickets.