0

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.

1 Answer 1

1

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

2
  • 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 highest comment_ID for particular post_id THANKS
    – Jeevan
    Commented May 22, 2014 at 5:15
  • You can use orderby value as comment_date_gmt for get latest comment at top. After getting its 0 element you get latest comment. Then comment->comment_author will give you author name for latest comment :)
    – ksr89
    Commented May 22, 2014 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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