Skip Navigation

[Resolved] Sort search results by comment count?

This thread is resolved. Here is a description of the problem and solution.

Problem:

The customer wants to sort results based on the comments count.

Solution:

Upon further review, I identified that the comment_count field in the wp_posts table can be used directly for sorting. I provided a code snippet to add to the theme's functions.php file to sort by the comment_count field:

add_filter('wpv_filter_query', function($query, $setting, $view_id){
    if (in_array($view_id, array(1234))) {
        $query['orderby'] = array('comment_count' => 'DESC');
    }
    return $query;
}, 101, 3);

Replace 1234 with the actual view's ID. This code sorts the results by the number of comments in descending order.

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

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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 2 replies, has 2 voices.

Last updated by Mateus Getulio 5 months, 3 weeks ago.

Assisted by: Mateus Getulio.

Author
Posts
#2704336

a.R

Hi, how do that? Thank you 🙂

#2704362

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

I'm afraid that there's no built-in feature that would allow you to sort the results based on the comments count.

As a workaround you could populate a custom field with the comments number using https://developer.wordpress.org/reference/functions/wp_count_comments/ and sort it using the newly added custom field.

Ideally you'd use cron to run the script that populates the custom field with the number of comments periodically, eg.: once a day.

Please check: https://toolset.com/forums/topic/how-to-create-custom-query-or-ordering-in-views-loop-results/#post-435106

Please have in mind that giving support to custom code is out of the scope of this forum. The hints I shared could point you in the right direction, but we can't create, debug or modify custom code for you and it's your responsibility to maintain it.

If implementation becomes a roadblock, you might want to explore the list of Toolset Contractors for assistance: https://toolset.com/contractors/

Kind regards,
Mateus

#2704875

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

Just a quick follow-up regarding my last reply.

Upon further review, I checked that there's a field comment_count available within the wp_posts table that can be used for this. It is possible to sort it using this field and some custom code, you can use the wpv_filter_query to change the orderby parameter, for example:

Please add the following code to your theme's "functions.php" file:

add_filter( 'wpv_filter_query', function($query, $setting, $view_id){
	if(in_array($view_id,array(1234))){		
		$query['orderby'] = array('comment_count' => 'DESC');
	}
	return $query;
}, 101, 3);

Please replace 1234 with your actual view's ID and test it to see if it is ordering it as expected.

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters