Getting $comments outside the comment template

$comments, or $wp_query->comments, is initialized by comments_template(), which you call in your template file when you want to load the comment sub-template file. So at the time of template_redirect it is not yet initialized. As Chris said, you should call get_comments() and pass it the post_id of your current post.

If you’re doing AJAX calls, even not from the admin side, you can use wp-admin/admin-ajax.php and use special actions hooks. This shortcuts the usual post queries, which you don’t need anyway.

Leave a Comment