1

The wordpress built-in get_comments() can easily list out comments base on post type, post meta, post id or comment author id. But I would like to bring them togather, list out recent received comments from posts of specific author.

My current way can only simply list all comments out:

    <?php
            $store_ID = $store_user->ID;
            $args = array(
                'status' => 'approve',
                'order' => 'DESC',
                'orderby' => 'comment_date_gmt',
                'meta_query' => array(
                            array(
                                'key' => 'rating',
                                'value'   => array(''),
                                'compare' => 'NOT IN'
                            ) 
                )
            );
            $comments = get_comments($args);
            if ( count( $comments ) == 0 ) {
                        echo '<span colspan="5">None message here</span>';
            } else {foreach ( $comments as $single_comment ) {
                        echo 'loop content';
            }
    ?>

So I am looking for the part to specify the loop stay inside posts of sepcific author. May be some sql skills are needed?

0

Nope, you can do that by nested query.

To get all comments of posts by a user, you should think about getting all post of that user first, then get the comments of that post.

<?php
    $args = array(
        'author' => AUTHOR_ID,
        'posts_per_page' => 500, //Don't use -1 here
    );
    $the_query = new WP_Query( $args );
    if($the_query->have_posts() ) : ?>
        <?php while ( $the_query->have_posts() ) : ?>
            <?php
            $nested_args = array(
                'post_id' => get_the_ID()
            );
            $comments_query = new WP_Comment_Query;
            $comments = $comments_query->query( $nested_args );
            if ( $comments ) {
                foreach ( $comments as $comment ) {
                    echo '<p>' . $comment->comment_content . '</p>';
                }
            }
        <?php endwhile; ?>
    <?php endif; ?>
<?php wp_reset_query(); ?>
  • That's the thing I want to achieve, gonna need some modification to make it run, thank you so much. – Cwkin7 Apr 14 '16 at 2:36
0

You can add an additional parameter to your query to get only those for a specific author by id or name

 $args = array(
            'status' => 'approve',
            'order' => 'DESC',
            'orderby' => 'comment_date_gmt',
            'author' => 12,
            'meta_query' => array(
                        array(
                            'key' => 'rating',
                            'value'   => array(''),
                            'compare' => 'NOT IN'
                        ) 
            )
        );

Source : WP query

Show Posts for one Author

Display posts by author, using author id:

$query = new WP_Query( array( 'author' => 123 ) ); Display posts by author, using author 'user_nicename':

$query = new WP_Query( array( 'author_name' => 'rami' ) );

  • umm, kinda misunderstanding. This is getting the comment author, not fit this case. Thank you for the helping anyway! – Cwkin7 Apr 14 '16 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.