1

All of a sudden I started getting memory and time out errors whenever I try to make a loop with WP_Query. Heres the code:

$args = array(
    'post_type' => 'post',
    'posts_per_page' => '3',
    'fields' => 'ids'
);

$query = new WP_Query($args);

 while($query->have_posts()) {

 }

Looping through 3 rows is obviously not going to overload the server so theres some kind of error happening. Like I said, this only started happening all of a sudden, it was working fine earlier. When it first broke, it was memory errors I was getting, but now its this same error:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Projects\fieldaware.dev\wp-includes\query.php on line 3760

It happens with all post types. I can view posts through the admin post editor, so it seems to be just my code causing this issue.

Is there any way I can diagnose whats going on here? Also I should mention that using get_posts doesn't cause any time out errors, so this:

$query = get_posts($args);

 foreach($query as $post) {

 }

works fine, its just WP_Query thats causing the problems.

UPDATE:

I tried running the exact code which was working before, and it works again, I don't get it, here it is:

    $args = array(
    'post_type' => 'fa_asset',
    'posts_per_page' => 50,
    'paged' => 1
);

$query = new WP_Query($args);

$data = array('data' => array());
//      ID, Model, Manufacturer, Serial Number, Last Service, Service Frequency, Next Service, Type

if ($query->have_posts()) {

    while($query->have_posts()) {
        $query->the_post();
        echo '<p>TEST</p>';

        $id = $query->post->ID;

        $type = $query->post->post_title;
        $manufacturer = get_post_meta($id,'manufacturer',true);
        $serial_number = get_post_meta($id,'serial_number',true);

        $data['data'][] = array(
            'id',
            'model',
            $manufacturer,
            $serial_number,
            'last service',
            'service frequency',
            'next service',
            $type
        );
    }

}
wp_reset_postdata(); 

echo "<pre>" . json_encode($data,JSON_PRETTY_PRINT). "</pre>";

I'm completely lost with this one (its not the post type BTW, I tried changing that). I can't see how anything is different with the more detailed code.

  • 1
    Is this the only loop in the page? Did you install any new plugins recently? I would try to find out what your issue is by elimination process, isolating the loop into a different template, or by removing plugins and so forth. – gdaniel Dec 8 '15 at 15:13
  • S.O.P asks : have you tried disabling plugins one by one to rule out an issue with a plugin? WP_Query can be hooked and filtered – Brandt Solovij Dec 8 '15 at 15:13
  • I didnt install any plugins, I'm building a theme and I'm trying to run the loop from inside the functions.php file. – Dominic Fagan Dec 8 '15 at 15:16
  • what action do you run your query on? is there anything inside your while loop? if you don't call the_post, your while loop never terminates. – Milo Dec 8 '15 at 15:17
  • Oh, I think that answers the question. My older code that works, I had $query->the_post inside the loop. So the issue was an infinite loop then. What exactly does the_post do? I thought its supposed to output something like the_content thats why I removed it. – Dominic Fagan Dec 8 '15 at 15:25
1

Yep the issue was what Milo mentioned, that I didn't have the_post inside the loop so the loop was running infinitely.

  • 1
    You should mark this as the answer ;) – jgraup Dec 11 '15 at 11:38

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.