Make WordPress Core

Opened 4 years ago

#52682 new defect (bug)

Total comment count is wrong with two meta queries on the same key

Reported by: salubritas's profile salubritas Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.6.2
Component: Comments Keywords:
Focuses: Cc:

Description

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”.

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
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.

Change History (0)

Note: See TracTickets for help on using tickets.