secret
Last active

How to use the pre_get_comments action to alter comment meta query vars

  • Download Gist
pre-get-comments.php
PHP
1 2 3 4 5 6 7 8 9 10 11
<?php
 
function cws_pre_get_comments( $comments ) {
$comments->query_vars['meta_key'] = 'foo';
$comments->query_vars['meta_value'] = 'bar';
// Because at this point, the meta query has already been parsed,
// we need to re-parse it to incorporate our changes
$comments->meta_query->parse_query_vars( $comments->query_vars );
}
 
add_action( 'pre_get_comments', 'cws_pre_get_comments' );

I think you need to do:

 $comments->meta_query = new WP_Meta_Query();

before doing

 $comments->meta_query->parse_query_vars( $comments->query_vars );

it works great without it

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.