I used get_comment()
but its parameter is $comment_ID
which I has to enter manually for previous comments from wp_comments
instead of that I need to get current comment means last updated comment author name based on the post_ID
. However wp_posts
and wp_comments
are related with post_ID
and comment_post_ID
, so I can use SQL query to get comment_ID I tried
$wpdb->get_results('select comment_ID from wp_comments where comment_post_ID = 1')
its even manual process but at-least I want to pass comment_ID by fetching from database but could't do that query results nothing so is there any way to get current comment_ID for a post_ID I can get post_id by $post->ID
.
Add a comment
|
1 Answer
Use get_comments( $args )
to get all the comments for a post.
$args= array(
'author_email' => '',
'ID' => '',
'karma' => '',
'number' => '',
'offset' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_id' => 0,
'post_author' => '',
'post_name' => '',
'post_parent' => '',
'post_status' => '',
'post_type' => '',
'status' => '',
'type' => '',
'user_id' => '',
'search' => '',
'count' => false,
'meta_key' => '',
'meta_value' => '',
'meta_query' => '');
You can set post_id
here and get all comments related to that post. It is same as your query.
$comments = get_comments(array('post_id'=> 'your post id'));
$comment_ids = array();
foreach($comments as $comment):
$comment_ids[] = $comment->comment_ID;
endforeach;
print_r($comment_ids); // edite print_r(comment_ids);
For reference http://codex.wordpress.org/Function_Reference/get_comments
-
I need to get comment author name when ever a new comment is generated for a particular post, although may be I could get the same from
get_comments()
function if I could access last comment, means highestcomment_ID
for particularpost_id
THANKS– JeevanCommented May 22, 2014 at 5:15 -
You can use
orderby
value ascomment_date_gmt
for get latest comment at top. After getting its0
element you get latest comment. Thencomment->comment_author
will give you author name for latest comment :)– ksr89Commented May 22, 2014 at 5:23