[wp-trac] [WordPress Trac] #52682: Total comment count is wrong with two meta queries on the same key

WordPress Trac noreply at wordpress.org
Mon Mar 1 11:09:49 UTC 2021


#52682: Total comment count is wrong with two meta queries on the same key
--------------------------+-----------------------------
 Reporter:  salubritas    |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Comments      |    Version:  5.6.2
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 The code below filters the comments shown in admin with a meta query. It
 works fine in that the comments in the list are filtered correctly, but it
 sends the results count way off (i.e. the one shown above the comment list
 table, to the right). For example, when there are 3 matching comments,
 above the table it says “45 items”.

 [[Image(https://i.imgur.com/yyXUSJE.png)]]

 I have traced this to the way the SQL is built in WP, with multiple outer
 joins to the meta table. When the query for comments is run, a GROUP BY is
 added so only unique IDs are returned. But when the count query is built,
 there is no GROUP BY so the count includes the meta rows and gets the
 wrong result.

 {{{#!php
 <?php
 function poc_comments_list_table_query_args ($args) {

         $meta_query = array (
                                                 'relation' => 'OR',
                                                 array (
                                                   'key' => 'my_meta_key',
                                                   'compare' => 'NOT
 EXISTS',
                                                 ),
                                                 array (
                                                   'key' => 'my_meta_key',
                                                   'value' => '',
                                                   'compare' => '=',
                                                 )
                                         );

         $args['meta_query'] = $meta_query;
         return $args;
 }
 add_filter ('comments_list_table_query_args',
 'poc_comments_list_table_query_args' );
 ?>
 }}}

 The intent is to return comments where either the meta value does not
 exist at all, or the meta value exists with a value of empty string.

 The result is the same if filtering pre_get_comments instead of
 comments_list_table_query_args (with the code adjusted of course as
 pre_get_comments passes a full WP_Comment_Query object instead of just the
 arguments).

 I found the following old tickets which seem somewhat related:

 https://core.trac.wordpress.org/ticket/29685
 https://core.trac.wordpress.org/ticket/23369

 Thanks.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/52682>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list