Skip Navigation

[Resolved] Filter on Number of Comments

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 2 replies, has 2 voices.

Last updated by aaronM-9 3 years, 1 month ago.

Author
Posts
#2189181

I would like to create a View that only shows posts with a minimum number of comments. Can you tell me how I might go about doing this with Toolset? Thanks.

- Aaron

#2189505

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The number of posts is stored as a standard field in wp_posts.

Views doesn't offer filters for standard posts in the UI, but you can use the wpv_filter_query API hook to add the argument yourself.

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Here's an example to limit a View results to posts with at least one comment:

function tssupp_filter_query($view_args, $view_settings, $view_id)
{
    if (in_array($view_id, array( 123 ))) { // Edit View ID

        $view_args['comment_count'] = array(
            'value' => '1',
            'compare' => '>='
        );
    }

    return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);
#2189801

Excellent. Thank you!