WordPress Backend Show Posts with specific number of comments

60 Views Asked by At

I created a function which displays posts with specific number of comments.

function wpse45436_posts_filter( $query ) {

    global $post_type, $pagenow; 
    if ($pagenow == 'edit.php' && $post_type == 'post') {

        if (isset($_GET['reviews'])) {

            $reviews_number = sanitize_text_field($_GET['reviews']);
            $query->query_vars['comment_count']['value'] = $reviews_number;
            $query->query_vars['comment_count']['compare'] = '>=';
        }
    }
}
add_action('pre_get_posts','wpse45436_posts_filter');

The code above gets a review number of 100 for example and then shows posts which have more than 100 comments.

However, I want to show posts between 100 and 150 comments for example. So, Is there any way I can add AND in the above code to define show posts with comments number?

1

There are 1 best solutions below

1
On

You may use something like this:

$query->query_vars['comment_count']['value'] = [100,150];
$query->query_vars['comment_count']['compare'] = 'BETWEEN';

BETWEEN is standard MySQL operator.