I have noticed that the default WP_Query object also contains all of a post's comments.

global $wp_query;

// Prints an object containing all of a post's comments

From my understanding, WordPress somehow combines a post query and a comments query into one WP_Query object, and then displays a post's comments by reading the $wp_query->comments object.

However, I need to query comments and order them by passing advanced query args. So I use WP_Comment_Query to create a new object that contains all of a post's comments in the order I need them.

$args = array(      
    'post_id' => get_the_ID(),
    'order' => 'ASC'

$comments_query = new WP_Comment_Query;
$comments = $comments_query->query($args);

// Prints an object containing all comments matching the query

But now it seems that using WP_Comment_Query basically makes WordPress load all of a post's comments twice.

Obviously, from a performance perspective, this is undesirable, seeing as I don't actually use the default WP_Query's comment object to display my comments.

So I'm wondering: Is it possible to prevent the WP_Query from grabbing a post's comments, so that I can later grab them 'manually' using WP_Comment_Query? And so, preventing the same comments from being grabbed from the database twice?

  • 1
    The $wp_query->comments is sort of explained here. You might wanna take a look into it.
    – Johansson
    Apr 12, 2017 at 20:13

1 Answer 1


Did you actually find a case where there are any real comments in the wp-query object after running a normal query?

If you examine the code in class-wp-query.php, you should find that the comments field only is populated with comments when a "comments-feed" is being queried for.

In normal operation, comments are not retrieved by the WP_Query, but instead retrieved by using WP_Comment_Query, and this does not happen until your theme calls the comments_template() function.

As such, you can use the 'comments_template_query_args' filter to filter the arguments passed to that class before the comments are displayed on the page. This won't affect the comments feed, because those are indeed populated by the main WP_Query, but only for the comments feed case.

  • After doing some more testing and inserting print_r($wp_query->comments); in various parts on my page, I found that the $wp_query->comments object is only filled after comments_template() is called. It seems that I might have misunderstood exactly when the $wp_query->comments object is filled, as I tested this inside my custom comments_template code. So just to make sure I understood your answer correctly, does this mean that running a custom WP_Comment_Query does in fact, not add an extra SQL query to my database when fetching comments?
    – Swen
    Apr 12, 2017 at 0:13
  • 1
    If you run a separate comments query instead of using the filter to modify the built in one, then yes, a separate query will be run. Again, examine the comments_template() code to see how it runs the comment query. You can use the filter to modify the built in one instead of running your own custom separate one.
    – Otto
    Apr 12, 2017 at 13:54
  • It seems that I unknowingly circumvented my problem by loading the comments with AJAX, and never actually calling comments_template in my theme. However, this information will still be useful when creating the no-js backup for the comment system! Thank you!
    – Swen
    Apr 12, 2017 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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