Support » Developing with WordPress » Comment count is wrong in admin with meta_query added

  • Resolved salubritas

    (@salubritas)


    Either there is something wrong with this code, or there is a bug in WP core.

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

    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.

    Looks like a bug to me, but maybe I have constructed the meta query wrongly?

    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.

    I have stripped this out of the actual code in use to get to a minimal example. The result is the same if filtering pre_get_comments instead.

    I found the following old tickets in Trac which seem somewhat related:

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

    Thanks. I can add to Trac if it seems like a bug.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Comment count is wrong in admin with meta_query added’ is closed to new replies.