diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 80f30e2a8a..ccd8464c51 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -482,10 +482,6 @@ class WP_Comment_Query { $this->meta_query = new WP_Meta_Query(); $this->meta_query->parse_query_vars( $this->query_vars ); - if ( ! empty( $this->meta_query->queries ) ) { - $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); - } - /** * Fires before comments are retrieved. * @@ -495,6 +491,12 @@ class WP_Comment_Query { */ do_action_ref_array( 'pre_get_comments', array( &$this ) ); + // Reparse query vars, in case they were modified in a 'pre_get_comments' callback. + $this->meta_query->parse_query_vars( $this->query_vars ); + if ( ! empty( $this->meta_query->queries ) ) { + $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); + } + // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); $last_changed = wp_cache_get( 'last_changed', 'comment' ); diff --git a/tests/phpunit/tests/comment/query.php b/tests/phpunit/tests/comment/query.php index 0d7269c8f0..420d8f2dad 100644 --- a/tests/phpunit/tests/comment/query.php +++ b/tests/phpunit/tests/comment/query.php @@ -1679,4 +1679,62 @@ class Tests_Comment_Query extends WP_UnitTestCase { $this->assertSame( $num_queries, $wpdb->num_queries ); } + + /** + * @ticket 32762 + */ + public function test_it_should_be_possible_to_modify_meta_query_using_pre_get_comments_action() { + $comments = $this->factory->comment->create_many( 2, array( + 'comment_post_ID' => $this->post_id, + ) ); + + add_comment_meta( $comments[1], 'foo', 'bar' ); + + add_action( 'pre_get_comments', array( $this, 'modify_meta_query' ) ); + + $q = new WP_Comment_Query( array( + 'comment_post_ID' => $this->post_id, + 'fields' => 'ids', + ) ); + + remove_action( 'pre_get_comments', array( $this, 'modify_meta_query' ) ); + + $this->assertEqualSets( array( $comments[1] ), $q->comments ); + } + + public function modify_meta_query( $q ) { + $q->meta_query = new WP_Meta_Query( array( + array( + 'key' => 'foo', + 'value' => 'bar', + ), + ) ); + } + + /** + * @ticket 32762 + */ + public function test_it_should_be_possible_to_modify_meta_params_using_pre_get_comments_action() { + $comments = $this->factory->comment->create_many( 2, array( + 'comment_post_ID' => $this->post_id, + ) ); + + add_comment_meta( $comments[1], 'foo', 'bar' ); + + add_action( 'pre_get_comments', array( $this, 'modify_meta_params' ) ); + + $q = new WP_Comment_Query( array( + 'comment_post_ID' => $this->post_id, + 'fields' => 'ids', + ) ); + + remove_action( 'pre_get_comments', array( $this, 'modify_meta_params' ) ); + + $this->assertEqualSets( array( $comments[1] ), $q->comments ); + } + + public function modify_meta_params( $q ) { + $q->query_vars['meta_key'] = 'foo'; + $q->query_vars['meta_value'] = 'bar'; + } }