2017-07-25 110 views
2

查询意见,我用下面的代码(简化)显示的最后10条评论列表:WordPress的 - 通过邮局元

<?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键和val UE。

任何消耗?

回答

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 
); 

试试这个,然后让我知道结果。

0

我的第一个问题在这里在Stackoverflow和完美的工作。

非常感谢Souvik!

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

$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>'; 
};