0

I'm trying find a way in my single.php to list all users (display name) that haven't commented on the current post.

I have this snippet that lists all the users who have commented...

/**
 * Retrieve a list of comment authors' names.
 *
 * @param  int    $id  Optional. Post ID.
 * @param  string $sep Optional. List separator.
 * @return string|null Names list.
 */
function get_comment_authors_list( $id = 0, $sep = ', ' ) {
    $post_id = $id ? $id : get_the_ID();
    if ( $post_id ) {
        $comments = get_comments( array(
            'post_id' => $post_id,
            'status'  => 'approve',
            'type'    => 'comment',
        ) );

        $names = array();
        foreach ( $comments as $comment ) {
            $name = $comment->comment_author;
            if ( $comment->user_id ) {
                $user = get_userdata( $comment->user_id );
                $name = $user ? $user->first_name : $name;
            }

            $arr = explode( ' ', trim( $name ) );
            if ( ! empty( $arr[0] ) && ! in_array( $arr[0], $names ) ) {
                $names[] = $arr[0];
            }
        }
        unset( $comments );

        $sep = $sep ? $sep : ', ';
        return implode( $sep, $names );
    }
}

/**
 * Display a list of comment authors' names.
 *
 * @param  int    $id  Optional. Post ID.
 * @param  string $sep Optional. List separator.
 * @return null
 */
function the_comment_authors_list( $id = 0, $sep = ', ' ) {
    echo get_comment_authors_list( $id, $sep );
}

/*
 * Available parameters for the Shortcode:
 *   int    post_id  Optional. Post ID.
 *   string list_sep Optional. List separator.
 *
 * Usage examples:
 *   [comment_authors_list /]
 *   [comment_authors_list post_id="1" list_sep=" · " /]
 */
add_shortcode( 'comment_authors_list', 'comment_authors_list_shortcode' );

/**
 * Shortcode callback function.
 *
 * @see get_comment_authors_list()
 * @return string|null Names list.
 */
function comment_authors_list_shortcode( $atts = array() ) {
    $atts = shortcode_atts( array(
        'post_id'  => 0,
        'list_sep' => '',
    ), $atts );

    return get_comment_authors_list( $atts['post_id'], $atts['list_sep'] );
}
2
  • I add <?php echo get_comment_authors_list(); ?> into my single.php to display all the comment authors
    – Pete
    Mar 9, 2018 at 6:25
  • Sorry, dumb comment. Misread function name. Ignore me. Mar 9, 2018 at 6:49

1 Answer 1

0

OK, so you want to get set complements from MySQL.

One way to do this will be with query like that:

SELECT u.ID
FROM
    wp_users  u
    LEFT OUTER JOIN wp_comments c ON
        (u.ID = c.user_id AND comment_post_ID = <YOUR_POST_ID>)
WHERE c.user_id is null

But you should remember, that such query won't be very efficient. So it will be OK, if your DB isn't very big. You should think of some other solution otherwise.

6
  • I'd rather a wp function like above rather than sql
    – Pete
    Mar 10, 2018 at 11:03
  • But the function you mentioned (get_comment_authors_list) is not a WP function. It comes from your theme or some plugin. Tele is no such function in WP core. Just write a function that uses this SQL query. Mar 10, 2018 at 11:06
  • Ok what I mean is i'd rather have a function like above rather than direct sql
    – Pete
    Mar 10, 2018 at 11:08
  • Sorry I didn't paste the whole snippet, I've updated the snippet
    – Pete
    Mar 10, 2018 at 11:10
  • Woah... Don’t do this like that. It’s so inefficient. You query all comments just to get authors names... So if you’d like to get all users without comments, then you had to get all users, get all comments, and for every user check if he commented. That will kill the performance... Mar 10, 2018 at 11:16

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.