Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
This is confusing. Please try to clarify what you want. Thanks. –  s_ha_dum Dec 14 '12 at 19:18

1 Answer 1

up vote 0 down vote accepted
  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' );
share|improve this answer
    
Thanks much @toscho ! :) –  Vô Danh Vô Hình Dec 15 '12 at 7:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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