Wordpress - Query comments by post meta -
i using code below (simplified) display list of last 10 comments:
<?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 $comment ) { echo '<p>'; echo get_the_title($comment->comment_post_id) . '<br>'; //post title echo $comment->comment_content; // comment content echo '</p>'; }; ?>
question:
well, meta_key , meta_value seem associated comment_meta... in case, have display comments based on post_meta key , value.
any sugestions?
you can try code. need add query posts array of post ides meta key. use array comments query argument.
//query posts meta key , 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 $args = array( 'post_type' => 'tarefa', 'number' => '10', 'order' => 'desc', 'orderby' => 'comment_date', 'post__in' => $posts_array, //this array of post ids meta query );
try , let me know result.
Comments
Post a Comment