2

我正在使用下面的代码(简化)来显示最后 10 条评论的列表:

<?php 

$args = array(
    'post_type'      => 'tarefa',
    'number'         => '10',
    'order'          => 'DESC',
    'orderby'        => 'comment_date',
    //'meta_key'        => 'field_name',
    //'meta_value'      => 'field_value',
);

$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

foreach ( $comments as $comment ) {
    echo '<p>';
    echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
    echo $comment->comment_content; // comment content
    echo '</p>';
};

?>

问题:

好吧,meta_keymeta_value似乎与comment_meta相关联......但就我而言,我必须根据post_meta键和值显示评论。

有什么建议吗?

4

2 回答 2

1

你可以试试这段代码。您需要为帖子添加查询以获取带有元键的帖子 ide 数组。然后将该数组用于评论查询参数。

//QUERY FOR POSTS WITH META KEY AND VALUE (META QUERY)
$post_args = array(
    'post_type'  => 'post',
    'meta_key'     => 'meta key',//Meta key of post
    'meta_value'   => 'meta value',//String or Numeric value
    'meta_compare' => '=',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
    while ( $post_query->have_posts() ) {
        $post_query->the_post();

        $posts_array[] = get_the_ID(); //Array of post ids

    }
    wp_reset_postdata();
}



//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
    'post_type'      => 'tarefa',
    'number'         => '10',
    'order'          => 'DESC',
    'orderby'        => 'comment_date',
    'post__in'        => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);

试试这个,然后告诉我结果。

于 2017-07-26T10:55:50.973 回答
0

我在 Stackoverflow 上的第一个问题,效果很好。

非常感谢你,苏维克!

下面的最终结果(简化):

$post_args = array(
  'post_type'              => 'tarefa',
  'posts_per_page'         => -1,
  'meta_key'               => 'field_name',
  'meta_value'             => 'field_value',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
    while ( $post_query->have_posts() ) {
        $post_query->the_post();
        $posts_array[] = get_the_ID(); //Array of post ids
    }
    wp_reset_postdata();
}

//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
    'number'         => '30',
    'order'          => 'DESC',
    'orderby'        => 'comment_date',
    'post__in'        => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);

$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

foreach ( $comments as $comment ) {
    echo '<p>';
    echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
    echo $comment->comment_content; // comment content
    echo '</p>';
};
于 2017-07-26T15:32:18.917 回答