$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp_query comment and meta query|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp_query comment and meta query

matteradmin9PV0评论

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' => '>=',
),

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far