最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Widget recent comment filter by post meta_value

programmeradmin5浏览0评论

How to filter recent comment by post meta value. I have a key “target” in post_meta, and I want to display in my new recent comment widgets.

My code : $comment = get_comments( array( ‘number => $instance[‘number’], ‘meta_query’ => array( ‘meta_key‘ => ‘target’, ‘meta_value’ => array(‘public’) ) );

I think the request value isn’t the post value.

Thx

How to filter recent comment by post meta value. I have a key “target” in post_meta, and I want to display in my new recent comment widgets.

My code : $comment = get_comments( array( ‘number => $instance[‘number’], ‘meta_query’ => array( ‘meta_key‘ => ‘target’, ‘meta_value’ => array(‘public’) ) );

I think the request value isn’t the post value.

Thx

Share Improve this question asked Jul 1, 2020 at 12:55 David StickerDavid Sticker 185 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
$post_args = array(
  'post_type'              => 'post',
  '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 );

Don't forget to update the test meta key and test meta value by your meta key and meta value.

发布评论

评论列表(0)

  1. 暂无评论