Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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...

share|improve this question
1  
post your code - it'll make it easier to help. –  anu Nov 27 '10 at 18:26
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
thanks, I'll just override that template –  Alex Nov 28 '10 at 6:55
    
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
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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