Posts Tagged ‘comment’

//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

Recently I just added image with comment(user can set image when he/she entered his/her comment)..My strategy is when comment is being saved just add image id with comment meta after get attachment_id..:)
But problem is when admin delete comment ..comment meta is not deleted..
Look at the function wp_delete_comment(). It fires an action before the comment is deleted:

/**
* Fires immediately before a comment is deleted from the database.
*
* @since 1.2.0
*
* @param int $comment_id The comment ID.
*/
do_action( ‘delete_comment’, $comment_id );

… and one after deletion:

/**
* Fires immediately after a comment is deleted from the database.
*
* @since 2.9.0
*
* @param int $comment_id The comment ID.
*/
do_action( ‘deleted_comment’, $comment_id );

So you can bind your callback to that:

add_action( ‘deleted_comment’, function( $comment_id ) {
delete_comment_meta( $comment_id, ‘your_meta_key’ );
} );