最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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;
Post a comment

comment list (0)

  1. No comments so far