1

How to get a variable comment field of all comments of current post?

I want to display all (for current post) comment user website url in to current post.

Thanks for help.

  • This is confusing. Please try to clarify what you want. Thanks. – s_ha_dum Dec 14 '12 at 19:18
1
  1. To get the comments use get_comments() and the current post’s ID.
  2. To get the URL fields only use wp_list_pluck().
  3. To remove the empty fields use array_filter() without a callback.

.

$comment_urls = array ();
$all_comments = get_comments( array ( 'post_id' => get_the_ID() ) );

if ( $all_comments )
{
    $comment_urls = array_filter( 
        wp_list_pluck( $all_comments, 'comment_author_url' ) 
    );
}

$comment_urls is now an array of URLS. Don’t forget to use esc_url() when you print those into your page.

You could also filter 'comments_clauses' to query for comments with a non-empty URL field only. That might be faster.

add_filter( 'comments_clauses', 'wpse_66056_get_urls_only' );
function wpse_66056_get_urls_only( $sql )
{
    remove_filter( current_filter(), __FUNCTION__ );
    $sql['where'] = $sql['where'] . " AND comment_author_url != ''";
    return $sql;
}
$all_comments = get_comments(
    array (
        'post_id' => get_the_ID(),
    )
);
$comment_urls = wp_list_pluck( $all_comments, 'comment_author_url' );

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.