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 have few thousands posts and need to list all of them in the admin. When I used WP_Query to get them all at once I got a memory error. Then I split it into several query calls and it worked just fine with 256 MB limit. Here is the code:

$limit = 200;
$offset = 0;
while (TRUE) {
    $posts = new WP_Query(array(
            'posts_per_page'        => $limit,
            'offset'                => $offset,
            'post_type'     =>  $post_type,
            'orderby'       =>  'title',
            'order'         =>  'ASC',
            'post_status' => array('publish', 'private', 'draft', 'inherit'),
    ));

    if (!$posts->have_posts()) break;

    while($posts->have_posts())
    {
            $posts->the_post();

            # do some echoing with the $post
    }

    $offset += $limit;
} /*(TRUE)*/

So this works just fine on development server. But when moved to production, it throws memory error Allowed memory size of 268435456 bytes exhausted (tried to allocate 64 bytes) ...

I tried lowering the $limit but it doesn't help (on dev it actually works just fine with $limit = 500.

What else can I do to force it to release memory?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

The posts are still held in memory under WordPress' cache mechanism (even though you replace $posts on every loop) - delete each one after operating on it:

# do some echoing with the $post

# wipe post from memory
wp_cache_delete( $post->ID, 'posts' );
wp_cache_delete( $post->ID, 'post_meta' );

Pro tip: save some needless queries with no_found_rows => true in your query args.

And if you're not using categories/tags in your loop, save even more queries (& memory) with update_post_term_cache => false in your query args.

share|improve this answer
    
That did it! Although the update_post_term_cache and no_found_rows options didn't do a notable difference in timing (about 7 sec to loop though all 4500 posts), but thats no major issue. –  frnhr Nov 6 '12 at 9:32

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.