WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;


            # 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
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


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.