Get the latest comment from a custom post type where depth = 1?

admin2025-06-06  9

Happy new year everyone.

I've been struggling with this one for a while. I need to get the latest comment from a custom post type, but ignore any child comments i.e. only look at comments with a depth of 1.

I can get the latest comment from a custom post type using:

$args = array(
    'post_type' => 'custom-post-type',
    'number' => '1',
    'orderby' => 'date',
    'order' => 'DESC'
  );

$comments = get_comments($args);
foreach($comments as $comment) :
    echo($comment->comment_author . '<br />' . $comment->comment_content);
endforeach;

However, it doesn't take into account the depth requirement. Any ideas how I can achieve this?

Happy new year everyone.

I've been struggling with this one for a while. I need to get the latest comment from a custom post type, but ignore any child comments i.e. only look at comments with a depth of 1.

I can get the latest comment from a custom post type using:

$args = array(
    'post_type' => 'custom-post-type',
    'number' => '1',
    'orderby' => 'date',
    'order' => 'DESC'
  );

$comments = get_comments($args);
foreach($comments as $comment) :
    echo($comment->comment_author . '<br />' . $comment->comment_content);
endforeach;

However, it doesn't take into account the depth requirement. Any ideas how I can achieve this?

Share Improve this question asked Jan 1, 2013 at 12:29 ChrisChris 4532 gold badges15 silver badges31 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Set the "parent" parameter as 0.

$args = array(
    'parent' => 0,    
    'post_type' => 'custom-post-type',
    'number' => '1',
    'orderby' => 'date',
    'order' => 'DESC'
  );

$comments = get_comments($args);
foreach($comments as $comment) :
    echo($comment->comment_author . '<br />' . $comment->comment_content);
endforeach;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749224495a317404.html

最新回复(0)