2 Ways to Get Comment Depth

I noticed that WordPress core does not have a function to get or to print depth level of a comment. WorPress database also doesn’t contain any information about comment depth (only ID of a parent comment). So, what to do?

#comments  /   3

1. Get the comment depth by using global variables

I’m sure you know about wp_list_comments() function — it prints the comments on a website page.

Why this functions is cool?

  • First, this function runs a comment loop and sets global variable which contain the depth level of a current comment in the loop.
  • Second, wp_list_comments allows you to specify your own comment template by using callback parameter.

    In my_custom_comment_template() global variables $comment_depth and $GLOBALS['comment_depth'] will be set.

2. Get the comment depth by comment ID

Okay, but how can I get a comment depth level, if global variable $comment_depth is unavailable? If everything we have is comment ID for example.

I think this simple function can help you with that:

function rudr_get_comment_depth( $my_comment_id ) {
	$depth_level = 0;
	while( $my_comment_id > 0  ) { // if you have ideas how we can do it without a loop, please, share it with us in comments
		$my_comment = get_comment( $my_comment_id );
		$my_comment_id = $my_comment->comment_parent;
	return $depth_level;


echo rudr_get_comment_depth( 784 ); // print the depth level of comment 784

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 3

  • Thank you very much!!

  • I use recursive to get comment depth.

    function tfa_get_comment_depth( $comment_id, $depth = 0 ) {
        $comment = get_comment( $comment_id );
        if( $comment->comment_parent > 0 ) {
            $depth = tfa_get_comment_depth( $comment->comment_parent, $depth );
        return $depth;

    Please share your thoughs via comments.

  • Thank you so much. I didn’t think to look there.

    You have saved me a great deal of frustration!

Leave your question or feedback

Please, enter a comment
Please, enter a name
Incorrect email