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();
}
?>
  • 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 Dec 5 '18 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_posts.comment_count DESC LIMIT 0, 5' " but, I don't know well about SQL well.... – JJang Dec 5 '18 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 Dec 5 '18 at 18:30
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' => '>=',
),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.