Get a variable field of all comments of current post

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.

Read More

Thanks for help.

Related posts

Leave a Reply

1 comment

    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' );