Wordpress-按post meta查询评论
我使用下面的代码(简化)显示最后10条评论的列表:Wordpress-按post meta查询评论,wordpress,comments,post-meta,Wordpress,Comments,Post Meta,我使用下面的代码(简化)显示最后10条评论的列表: <?php $args = array( 'post_type' => 'tarefa', 'number' => '10', 'order' => 'DESC', 'orderby' => 'comment_date', //'meta_key' => 'field_name', //
<?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_键和meta_值似乎与评论meta相关。。。但在我的例子中,我必须显示基于post\u meta键和值的评论
有什么建议吗?你可以试试这段代码。
您需要添加一个post查询,以获得带有元键的post IDE数组。
然后在comments查询参数中使用该数组
//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
);
试试这个,然后告诉我结果。我在Stackoverflow的第一个问题,效果非常好 非常感谢你,苏维克 以下为最终结果(简化):
$post_args=数组(
“post_type”=>“tarefa”,
“每页帖子数”=>-1,
“元密钥”=>“字段名称”,
“元值”=>“字段值”,
);
$post\u query=新的WP\u查询($post\u args);
$posts_array=array();
如果($post\u query->have\u posts()){
而($post\u query->have\u posts()){
$post_query->the_post();
$posts_array[]=获取_ID();//post ID数组
}
wp_reset_postdata();
}
//您的评论ARGS应该如下所示
$args=数组(
“编号”=>“30”,
“订单”=>“描述”,
'orderby'=>'comment\u date',
'post\u in'=>$posts\u数组,//这是带有元查询的post id数组
);
$comments\u query=新的WP\u Comment\u查询;
$comments=$comments\u query->query($args);
foreach($comments作为$comment){
回声“”;
echo获取标题($comment->comment\u post\u ID)。“
”//post title
echo$comment->comment\u content;//注释内容
回声“”;
};
$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>';
};