Title: Where is $comments array created?  What file, what function?
Last modified: August 19, 2016

---

# Where is $comments array created? What file, what function?

 *  [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/)
 * I’m in my comments.php and I see that $comments is a global array that WP creates.
   Even if your theme uses cool new functions to get comments, $comments is still
   there whether you like it or not.
 * I want to know where it is created so I can take a look at the code. What is 
   the problem? Well, $comments is a HUGE array, it loads all of your comments, 
   even if you’re doing pagination or whatever. So if you have 1,000 comments on
   a post, it doesn’t make sense to have an array that big in memory.
 * I can fix the MySQL, that’s not my problem. I just can’t figure out where $comments
   is created. Various searches not helping me, $comments appears around 900 times
   in WP’s code and Google isn’t helping me either. All I need is a hint. Thanks.

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/page/2/?output_format=md)

 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340223)
 * I really think this belongs in WP-Advanced but I’m not a moderator.
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340251)
 * Ahh, looks like get_comments() is going to help me, looking into it…
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340257)
 * Here’s the fix, if you’re wondering why your blog loads up on a huge 10,000 comment
   $comments array every time someone visits your most popular page, it’s the get_comments()
   function here:
 * [http://phpxref.ftwr.co.uk/wordpress/wp-includes/comment.php.source.html#l171](http://phpxref.ftwr.co.uk/wordpress/wp-includes/comment.php.source.html#l171)
 * You’re looking for the “number” which sets the MySQL “LIMIT” so you can get $
   comments down to a reasonable size. You can hack that function or comments_template()
   in comment-template.php
 * Probably comment-template.php should set the “number” to whatever you have set
   in your admin under “Discussion” for comments per page, even if your theme has
   no idea what pagination is.
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340260)
 * I won’t check “resolved” because I think the “number” LIMIT should be set from
   the admin panel somewhere, with some reasonable default like LIMIT 100.
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340261)
 * If I have time, I will report back here tomorrow if this reduced stress on my
   server.
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340263)
 * Here’s the exact change I made, in comment-template.php, after “else if”. For
   blogs that don’t require logins, most of your traffic will see what is selected
   by this “else if” because there is no $comment_author most of the time. I changed
   to DESC to save my theme from reversing the array, but you may have a different
   theme. If there’s a better way to do it, let me know.
 * —
 * /** [@todo](https://wordpress.org/support/users/todo/) Use API instead of SELECTs.*/
   
   if ( $user_ID) { $comments = $wpdb->get_results($wpdb->prepare(“SELECT * FROM
   $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = ‘1’ OR ( user_id
   = %d AND comment_approved = ‘0’ ) ) ORDER BY comment_date_gmt”, $post->ID, $user_ID));}
   else if ( empty($comment_author) ) { $comments = get_comments( array(‘post_id’
   => $post->ID, ‘status’ => ‘approve’, ‘order’ => ‘DESC’, ‘number’ => ’50’) ); }
   else { $comments = $wpdb->get_results($wpdb->prepare(“SELECT * FROM $wpdb->comments
   WHERE comment_post_ID = %d AND ( comment_approved = ‘1’ OR ( comment_author =%
   s AND comment_author_email = %s AND comment_approved = ‘0’ ) ) ORDER BY comment_date_gmt”,
   $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));}
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340266)
 * Ugh, it’s not working. I’m wondering if that big array is cached now?
 *  [Clayton James](https://wordpress.org/support/users/claytonjames/)
 * (@claytonjames)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340273)
 * > if you’re wondering why your blog loads up on a huge 10,000 comment $comments
   > array every time someone visits your most popular page,…
   > …I won’t check “resolved” because I think the “number” LIMIT should be set 
   > from the admin panel somewhere, with some reasonable default like LIMIT 100.
 * I’m not sure if we’re on the same page (pardon the pun) or not, but is any of
   this useful in any way?
 * [File:options-discussion1.png](http://codex.wordpress.org/images/5/55/options-discussion1.png)
 * My attention was drawn to the part about:
 * “Other comment settings”
 * The only other thing I wonder is, does a theme need to be coded to take advantage
   of those features?
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340274)
 * Never mind, it works, but not if you’re logged in.
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340275)
 * “is any of this useful in any way?”
 * That option you posted doesn’t affect the memory requirement of $comments, which
   is my entire reason for starting this thread. The option you’re talking about
   only affects the pagination of newer themes, but even if you have a more updated
   theme, $comments still sucks up memory if you have a lot of comments.
 * Unless you make the change I posted, your blog loads ALL of your comments into
   the $comments array, which is probably not a big deal for most blogs. But I have
   one post with 9000+ comments and it’s costing me a lot of cash to host that particular
   page, so I’m looking for ways to save memory, CPU.
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340276)
 * To test if it’s working, add “echo count($comments);” somewhere at the top of
   your comments.php theme file.
 *  [Clayton James](https://wordpress.org/support/users/claytonjames/)
 * (@claytonjames)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340277)
 * Ahhhh… I see. Thanks for the enlightenment. Got a link to your site? I would 
   be interested to take a look at it.
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340279)
 * If you have any questions, let me know [pj@pjbrunet.com](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/pj@pjbrunet.com?output_format=md)
 * [http://www.ferodynamics.com/](http://www.ferodynamics.com/)
 *  Thread Starter [PJ Brunet](https://wordpress.org/support/users/knowingart_com/)
 * (@knowingart_com)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340380)
 * Update: I’m wondering if WP developers just assume high-traffic sites use some
   kind of cache now, allowing them to ignore things like this?
 * I was watching a video from WordCamp 2007 telling plugin developers to test on
   large datasets first. So why wasn’t get_comments tested with lots of comments?
 * Because this code is so bloated now (I would edit wp-includes all day long but
   then I could never upgrade) I’m trying wp-cache (also recommended in that same
   video) and the plugin works for a little while and then breaks. Then all pages
   stop loading until wp-cache is turned off. Submitting comments with wp-cache 
   sometimes gives PHP errors due to missing cache files. Comments do not update
   until the cache refreshes (what?!)
 *  [fiunchinho](https://wordpress.org/support/users/fiunchinho/)
 * (@fiunchinho)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/#post-1340412)
 * KnowingArt_com , I have the same problem.
 * I tried your hack and it didnt work for me. It’s like it doesnt use get_comments
   at all. I tried adding die(“wtf”); in the very first line of get_comments() and
   the function works normally. I dont see any “wtf”. It’s not using get_comments.
 * So I guess it’s not going through the “else if”, but the others, that are using
   $wpdb->get_results():
 *     ```
       /** @todo Use API instead of SELECTs. */
       	if ( $user_ID) {
       		$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) )  ORDER BY comment_date_gmt", $post->ID, $user_ID));
       	} else if ( empty($comment_author) ) {
       		$comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC', 'number' => '5') );
       	} else {
       		$comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));
       	}
       ```
   
 * Any suggestions?

Viewing 15 replies - 1 through 15 (of 21 total)

1 [2](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/page/2/?output_format=md)

The topic ‘Where is $comments array created? What file, what function?’ is closed
to new replies.

## Tags

 * [Comments](https://wordpress.org/support/topic-tag/comments/)
 * [limit](https://wordpress.org/support/topic-tag/limit/)

 * 21 replies
 * 5 participants
 * Last reply from: [yashmistrey](https://wordpress.org/support/users/yashmistrey/)
 * Last activity: [15 years, 5 months ago](https://wordpress.org/support/topic/where-is-comments-array-created-what-file-what-function/page/2/#post-1340545)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
