I'm displaying the comments for a post my way, using $comm = get_comments() and then passing $comm to wp_list_comments() (this is the reason why).

How to disable WordPress from loading comments from the database into $wp_query, when the comment template is being displayed ? I'm asking this because I want to avoid this extra database call, since I'm getting the comments myself using the above method.

PS: I also noticed the $comments global variable is a array containing ALL the comments for a post, not just the ones from the current comment page. If I have a post with 10K comments, WP will load all these at once? Seems ridiculous...

  • 1
    post your code - it'll make it easier to help. – anu Nov 27 '10 at 18:26

You can't prevent comments_template() from making an SQL query. Well, you could maybe hook into the DB layer to prevent just that specific query, but that would be very cumbersome to do. If this is for a theme, you could just remove the call to comments_template() and replace it with your own function. comments_template() doesn't do that much: just load all the comments, maybe also the just-submitted comment if it isn't approved yet, separate them into pings, trackbacks and regular comments if needed, and load the comments template file. Comments paging is relatively young in WordPress, so it's probably not as efficient as it could be yet.

  • ok. I ran into something weird :) If I replace WP's sql query with my get_comments() function I get 10 extra database queries reported, but less memory usage. For eg. for a post with 5000 comments, memory usage is 32 MB vs 16 MB. I don't understand how can WordPress make less database queries by selecting 5000 comments than me using get_comments() to pull out only 10 comments.... – Alex Nov 29 '10 at 10:40
  • Even more weird is if I use the same sql query as WP does, but append add "LIMIT 10" to it. query count increases by 10... – Alex Nov 29 '10 at 10:42
  • @Alex: Can you post your code in a pastebin and share it with us? Maybe WordPress does some caching, and you don't hit the cache because your query with LIMIT 10 is different. – Jan Fabry Nov 29 '10 at 11:04
  • well it's pretty much the same as the default function: pastebin.com/DuEkCZDv if you call it with ajax = true it will get only 10 comments, otherwise it will run the sql which gets all the comments... – Alex Nov 29 '10 at 13:22
  • anyway, I think that I'm forced to use the original query for all comments, because otherwise I'll get issues with threaded comments. WP has a pretty bad comment system... – Alex Nov 29 '10 at 13:31

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.