简体   繁体   中英

WordPress Backend Show Posts with specific number of comments

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM