0

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?

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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