• 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 6 replies - 16 through 21 (of 21 total)
  • I added LIMIT 200 at the end of

    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 LIMIT 200", $post->ID, $user_ID));

    but it doesnt solve the problem, because now it’s like the post has JUST 200 comments, so it shows no pages or navigation. It just shows the first 200 comments.

    I need help plz

    I was trying to load a table with some 6000 records using that function ($wpdb->get_results()), and it wouldn’t do it, it would actually break the code, so I figured it had to do with the memory specified in your php.ini (memory_limit), when it is usually 32MB, my attempt to load the 6000 records with the get_results() function was loading a big array into memory (more than 55MB in memory usage), so PHP simply stopped running my code, however, I was able to load the array using OBJECT instead of ARRAY_A (associative arrays are heavier), but we want it to work either way, so I increased the value of “memory_limit” to fit my needs.

    I hope this helps.

    That would fix it for now, but what if the comments keep coming?

    The point is that it shouldnt take ALL the comments if you are showing the comments with pagination. Why do I need it?

    Any other ideas?

    The thing is that with the wordpress functions, pagination works with the entire array of results (comments in this case) and then starts loading them from the page you’re on, so for every page, the entire recordset is loaded then Walker::paged_walk sets the pointer to start loading from whatever page you’re on.

    If you’re looking to see where the $comments are created, see wp-includes/query.php, $this->comments is the entire array of comments then passed on to the different functions in comments-template.php and so on.

    A solution would be to load the comments based on your own pagination code, querying the database with the LIMIT X,Y and then output the HTML using the standard mysql functions from PHP.

    See https://www.onextrapixel.com/2009/06/22/how-to-add-pagination-into-list-of-records-or-wordpress-plugin/

    It looks like the only way around is to modify the SQL statement.

    For now, Im using this in comment-template.php:

    // Choose how many comments you want to take.
    $limit = 1000;
    // The actual query.
    $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' ) ) AND comment_ID >  ((SELECT MAX(comment_ID) FROM $wpdb->comments) - $limit) ORDER BY comment_date_gmt", $post->ID, $user_ID));

    Using this, it will only take the latest 1000 comments. So if you have pagination activated, it will work for those 1000 comments. Assuming that comments older than the latest 1000 comments are “useless”, this would do the trick.

    Anyway, there should be a better option.

    yashmistrey

    (@yashmistrey)

    when someone submit comment ! after submitting this message not showing
    like …“Your comment is awaiting moderation.”

    i don’t know friend what happened, some day ago it was working properly !

    plztell me if there any default setting ? for discussion ?

    here is the blog….
    https://www.shreshthbharat.in

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘Where is $comments array created? What file, what function?’ is closed to new replies.