Reputation: 1304
LAST UPDATE with SOLUTION: It was me using have_posts() twice in a row in my if/while combo, deep in the docs it says not to do that. See the accepted answer for the solution.
I should print something from each record to see if it's stuck on the first record, I'll do that next.
<?php
echo(wp_count_posts( 'attorney' )->publish . " Published attorney posts" . "\r\n");
$args = [
'post_type' => 'attorney',
'posts_per_page' => -1, // Get all advisor posts
];
$advisor_query = new WP_Query($args);
echo($advisor_query->found_posts . " Posts were found in the query" . "\r\n");
$x = 0;
if ($advisor_query->have_posts()):
while ($advisor_query->have_posts()):
echo("Loop " . $x++ . "/r/n");
echo("This is the memory usage during the input loop" . "\r\n");
echo("Memory used: " . memory_get_usage() . " bytes" . "\r\n");
endwhile;
endif;
echo("Yer Done");
Here's an image of the error happening, there's only 24 records in the WPQuery result but when I iterate over the records it runs forever. (the cut off number is also 24)
I'll probably try to stand this up locally again, was running into PHP-fpm issues locally.
If anyone has ideas why this would fail, it's on Hostinger shared hosting, I have the mem limit checked, it's set at 512M, I'm nowhere near that in mem usage.
Details
I'm writing a PHP page for testing on Hostinger. I copied the site files and database over and stood the site up there. The rest of the site runs fine.
The Problem:
I am running into a memory limit on this site, but my memory usage checks are showing only 9-13MB of memory being used, and the active memory limit on the site is 512MB.
The Question:
What simple PHP (or computer) thing am I hopefully not understanding here?
I can read, right? It's bytes vs Megabytes in the output in the linked image below? I'm comparing apples to apples?
I see for example 12604480 bytes in the output, that's 12.6MB right? Compared to 512MB, I don't see how that's the problem.
And if it's an endless loop, I can't see it.
Thanks for any help.
Full script at pastebin
Details before the MVCE:
I have this at the beginning, after the get_header() call:
echo 'Memory limit: ' . ini_get('memory_limit');
I have code such as this placed throughout the script:
echo("This is the memory usage during the input loop");
echo("Memory used: " . memory_get_usage() . " bytes");
wp_reset_postdata();
echo("This is the memory usage after resetting post data in the loop");
echo("Memory used: " . memory_get_usage() . " bytes");
You can see from these screenshots that I am not using more than 12-ish MB as long as my code is still printing it's usage along the way.
Image showing current memory limit and my usage
Image showing more memory usage until it stops printing
I just thought this might be an obvious PHP environment issue I am not aware of. I have a pretty decent software background in Computer Science, but not in industry, so I'm probably making a very dumb mistake.
For what it's worth there's only 24 records, these are employee records, there's only about 10 text fields, one of which is a link. I assume I am only working with the link, not directly storing the images in memory. If that was the case, that could be the problem.
Thanks for any feedback on my PHP code, I'm a Python and other langs guy but not a front-end dev obviously.
Did this research and more:
Hi, looked at: PHP out of memory error even though memory_limit not reached -- I'm testing this dev site on shared hosting (not locally long story). I don't think I can control the Apache2 memory limit, but I doubt it would be a problem. I'm only trying to load about 30 records here.
Also: https://stackoverflow.com/a/55566634/3255525 -- tried upping wp mem in the wp-config.php to 512m, no luck.
And: Fatal error: Out of memory, but I do have plenty of memory (PHP) -- which largely requires config access to Apache to fix, but I'm on Hostinger shared hosting for testing/building this site.
And: https://stackoverflow.com/a/46652352/3255525 -- the question is a bit confusing and the answer seems to be trying to patch the problem and not fix it. I could maybe explore this one more, I'll sleep on it.
Original updates
UPDATE: Based on comments I am going to investigate the server logs (didn't think of it on shared hosting duh). Also will narrow down a much better MVCE until I eliminate the problem. Should have that done within a few hours. Thanks for your comments!
UPDATE 2: I can't access any useful logs, /var/log just has some newrelic dirs with no files in them. There's a /opt/sentinelone, whatever that is, I can't access the logs there. Then there's /opt/alt/redis/var/log, which I also can't access, and are probably irrelevant.
UPDATE 3: Deleted previous updates and deleted original code, replacing with this MVCE, sorry for the delay:
UPDATE 4: New MVCE that shows the loop runs forever, this has to be me doing something stupid. I assume the while loop iterates over the WPQuery results, is that wrong?
Upvotes: 2
Views: 124
Reputation: 1304
I figured this out, despite there being bad examples all over the place, including apparently in the docs I link, bottom line is...
Answer: I needed to avoid raw repetitive uses of have_posts()
in my if
statement and the nested while
statement under the if
. This causes an endless loop.
The second query needs to be this in full:
while ($cpt_query->have_posts()) : $cpt_query->the_post();
See the Wordpress Developer Resources page for have_posts()
.
Here's the correct approach it's currently in the last comment:
<?php
// Define args
$args = array('post_type' => 'custom_post_type');
// Execute query
$cpt_query = new WP_Query($args);
// Create cpt loop, with a have_posts() check!
if ($cpt_query->have_posts()) :
while ($cpt_query->have_posts()) : $cpt_query->the_post(); ?>
<?php the_title(); ?>
Upvotes: 0