wp_query comment and meta query

admin2025-06-05  0

I making wp_query that control comment-count, and meta key value. So, I coded like this. but is doesn't work. When I use comment_cout only or meta_query only, it works good. when I use together, it doesn't work normally. Can I get some help? Thank you.

<?php
$arg = array(
    'post_type' => 'race',
    'posts_per_page' => 5,
    'comment_count' => array(
        array(
            'value' => 10,
            'compare' => '>=',
        ),
    ),
    'meta_query' => array(
        // 'relation' => 'AND',
        array(
            'key'     => 'race_date',
            'value'   => $today, //this is get by php time() function. ex) 2018-12-10
            'compare' => '>=',
        ),
    ),
    'orderby' => array(
        'comment_count' => 'DESC',                           
    ),
);

$query = new WP_Query( $arg );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $postid = get_the_ID();

        the_title();
        echo '<br />';
    }
    wp_reset_postdata();
}
?>

I making wp_query that control comment-count, and meta key value. So, I coded like this. but is doesn't work. When I use comment_cout only or meta_query only, it works good. when I use together, it doesn't work normally. Can I get some help? Thank you.

<?php
$arg = array(
    'post_type' => 'race',
    'posts_per_page' => 5,
    'comment_count' => array(
        array(
            'value' => 10,
            'compare' => '>=',
        ),
    ),
    'meta_query' => array(
        // 'relation' => 'AND',
        array(
            'key'     => 'race_date',
            'value'   => $today, //this is get by php time() function. ex) 2018-12-10
            'compare' => '>=',
        ),
    ),
    'orderby' => array(
        'comment_count' => 'DESC',                           
    ),
);

$query = new WP_Query( $arg );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $postid = get_the_ID();

        the_title();
        echo '<br />';
    }
    wp_reset_postdata();
}
?>
Share Improve this question asked Dec 5, 2018 at 0:44 JJangJJang 1031 silver badge10 bronze badges 3
  • Looks ok. I recommend var_dump($query->request); and take a look at the generated SQL. I don't think it will change anything but I always add "TYPE"=>"DATE" as an argument when I do a date based meta query. – jdp Commented Dec 5, 2018 at 1:13
  • Thanks for your reply. As you recommended, I did var_dump. it says.. " 'SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'race_date' AND wp_postmeta.meta_value >= '2018-12-05' ) ) AND wp_posts.post_type = 'race' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_postsment_count DESC LIMIT 0, 5' " but, I don't know well about SQL well.... – JJang Commented Dec 5, 2018 at 1:39
  • There you go. The query doesn't pick up comment_count because as @JacobPeattie notes below, the comment_count clause not a nested array. – jdp Commented Dec 5, 2018 at 18:30
Add a comment  | 

1 Answer 1

Reset to default 1

The inline documentation of WP_Query has this:

Filter results by comment count. Provide an integer to match comment count exactly. Provide an array with integer 'value' and 'compare' operator ('=', '!=', '>', '>=', '<', '<=' ) to compare against comment_count in a specific way.

So there should only be one array, not 2 like with a meta query:

'comment_count' => array(
    'value'   => 10,
    'compare' => '>=',
),
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749128214a316603.html

最新回复(0)