wpDiscuz - WordPress Comment Plugin
Share:
Notifications
Clear all

[Solved] filter comment with custom field


vahid13713
Posts: 4
Topic starter
(@vahid13713)
New Member
Joined: 4 months ago

Hello, How can I filter the comments displayed under a page based on a custom field? For example, if the custom field has a value of a, that comment should be displayed, and if it does not have this value, it should not be displayed.

Topic Tags
5 Replies
Asti
Posts: 4613
 Asti
Support
(@asti)
Support member
Joined: 4 years ago

vahid13713,

If you're a developers you can use the get_comments() and get_comment() functions. 

Also, we have plugin customization and custom addon development services at the gVectors Team. They can assess the work and create it for you. Just contact us via sales[at]gvectors.com email address and send a list of requirements you'd like to have.

4 Replies
vahid13713
(@vahid13713)
Joined: 4 months ago

New Member
Posts: 4

@asti 

Hello asti and thank you very much for your answer
I am a developer.

I get the comment with the  get_comments() function and I want wpdiscuz to show only the comments I received. How can I do this?  Please provide me with a sample code 

 

my sample code:

    $comments20 = get_comments (array ('meta_key'=> 'keytoopurl', 'meta_value'=> 'y'));

I want wpdiscuz to display only variable  comments20 

 

How can I do this?

 

 

Thanks for your cooperation

 

 

Asti
 Asti
Support
(@asti)
Joined: 4 years ago

Support member
Posts: 4613

@vahid13713,

Please use this code: 

add_action('pre_get_comments', function ($queryObj) {
            $vars = $queryObj->query_vars;
            $vars['meta_key'] = 'keytoopurl';
            $vars['meta_value'] = 'y';
            $queryObj->query_vars = $vars;
    });
vahid13713
(@vahid13713)
Joined: 4 months ago

New Member
Posts: 4

@asti 

 

Thank you very much for your answer
I tested this code but it does not work. All comments are displayed!

vahid13713
(@vahid13713)
Joined: 4 months ago

New Member
Posts: 4

@asti 

 

Hi, I have fixed the problem. I am posting content for others to use.
Your plugin caches comments, which is why our code does not work. Also, after writing the filter, it is necessary to activate and deactivate the plugin once. The problem was solved by adding the following line of code.

 

 

add_action('pre_get_comments', function ($query)
{
    $query->query_vars['meta_query'] = [
        [
            'key' => 'keyurl',
            'value' => "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"
        ]
    ];
}, 10, 1);
Share: