2017-07-25 27 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>'; 
};