1

Not long ago I learned how to add and filter comments on posts/pages with certain meta key/value and it works fine now. Now what I want is to do two things on the backend (/wp-admin):

  1. Filter the /wp-admin/edit-comments.php so it will not display any comments which has the certain meta keys (e.g. meta1, meta2) in there.
  2. Make a list in a custom menu page in the admin backend which will list comments like /wp-admin/edit-comments.php but will only show those comments that has the meta keys like above.

I've been searching around for something like this but only found stuffs like filtering the backend comment list by current user, etc. and not by comment meta keys. I've also never done anything in the backend other than adding some menu/submenu with functions and options so I don't know where to start on these filters, though I have a feeling this has something to do wit WP_Query functions.

1 Answer 1

0

1. You can use pre_get_comments hook:

add_action('pre_get_comments', function($query)
{
    global $pagenow;
    if ( is_admin() && ('your-custom-page' === $pagenow) ) {
        $query->query_vars['meta_query'] = [
            'relation' => 'AND',
            [
                'key' => 'key1',
                'value' => 'meta1'
            ],
            [
                'key' => 'key2',
                'value' => 'meta2'
            ]
        ];
    }
});

2. You need to learn how to create an admin list table.

Also, take a look at:

6
  • May I ask how should I edit the code so it will "filter out" anything that has a certain meta key from being listed? Commented May 27, 2016 at 7:09
  • @58YtQ2H83m17838963l61BU07Y8622 Yes, you can use 'compare' => '!='.
    – Minh Tri
    Commented May 27, 2016 at 7:34
  • May I ask how should I do the code from #1 if I want do these conditions: Filter the comments to only show items without meta keys 'meta1' and 'meta2' which can have any numeric meta values. Commented May 27, 2016 at 8:23
  • @58YtQ2H83m17838963l61BU07Y8622 You can use 'key' => 'meta1', 'type' => 'NUMERIC', 'compare' => 'NOT EXITS' without specify meta value. Do the same with meta2 in your meta query arguments.
    – Minh Tri
    Commented May 27, 2016 at 9:14
  • Also I want to ask about the part of the code: ('your-custom-page' === $pagenow) - How should I edit it so it will be a condition when in the admin page "/wp-admin/edit-comments.php" ? Commented May 27, 2016 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.