Retrieve recent comment in wordpress

Posted: September 24, 2015 in Uncategorized
Tags: , ,

//RECENT COMMENTS
$paged = ( get_query_var( ‘paged’ ) ) ? absint( get_query_var( ‘paged’ ) ) : 1;
$recent_comments = $wpdb->get_results(“select DISTINCT comment_post_ID from  wp_comments where comment_approved  =’1′    ORDER BY comment_ID DESC “);

if ( count( $recent_comments ) > 0 )
{

$post_ids = array();
foreach ( $recent_comments as $comment )
{
$post_ids[] = $comment->comment_post_ID;
$posts_ids_array=array_unique( $post_ids );
}

// Query for the posts with the IDs collected
$posts_with_recent_comments = new WP_Query(array(
‘post__in’ => $posts_ids_array,
‘paged’ => $paged,
‘orderby’=>’post__in’
));
// Get your loop on
if ( $posts_with_recent_comments->have_posts() )
{
while ( $posts_with_recent_comments->have_posts() )
{
$posts_with_recent_comments->the_post();
$post_id = $post->ID;
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$img =  wp_get_attachment_image_src( $post_thumbnail_id, ‘thumbnail’);
if(empty($img[0])){
$img[0] = get_template_directory_uri().’/images/noimage.gif’;
}
get_template_part( ‘content’, get_post_format() );
}

$big = 999999999;
echo paginate_links( array(
‘base’ => str_replace( $big, ‘%#%’, esc_url( get_pagenum_link( $big ) ) ),
‘format’ => ‘?paged=%#%’,
‘current’ => max( 1, get_query_var(‘paged’) ),
‘total’ => $posts_with_recent_comments->max_num_pages,
‘prev_text’          => __( ‘Previous page’, ‘twentyfifteen’ ),
‘next_text’          => __( ‘Next page’, ‘twentyfifteen’ ),
) );
wp_reset_query();
}

}

Advertisements
Comments
  1. thank’s tutorial this..very usefull

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s