I got a fatal error in existing code, when I debug deeper I found that it was coming from bellow line.

get_posts( array("posts_per_page"=>-1) );

Can anyone explain why is that and how to overcome it?

Error :

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /var/www/web0/html/blog/wp-includes/wp-db.php on line 2204
  • How many posts do you have, as your query fails due to it being to big. I suspect that you have more than 2000 posts you are trying to query Commented Jun 17, 2015 at 13:03
  • its about 750 posts Commented Jun 17, 2015 at 13:04
  • I suspect that they are quite large. Why do you need to query so many posts at once Commented Jun 17, 2015 at 13:06
  • actually I found it in Redux framework which I faced the issue with that. github.com/reduxframework/redux-framework/blob/master/ReduxCore/… --- line 951 Commented Jun 17, 2015 at 13:13
  • Then you should contact the relevant developer about this issue Commented Jun 17, 2015 at 13:23

2 Answers 2


Running a no limit query is very likely the cause of your memory issues. So you might want to change your query.

If still you need to do this way, you can use the fields option, it returns post IDs only and will save a lot of memory usage - example below:

$your_query = get_posts(
        'posts_per_page' => -1,
        'post_type'      => 'post',
        'fields'         => 'ids',
  • what if need more fields like post_title ? Commented Jun 17, 2015 at 13:17
  • even with this , it is giving the same results Commented Jun 17, 2015 at 13:26
  • The fields attribute doesn't change what you can return on your Query - it will just optimize to use less memory. If you're still having problems with that, you need to reduce the number of posts per page by using a pagination, otherwise your server will not be able to list all your 750 posts. Commented Jun 17, 2015 at 13:29

If your circumstances require getting all posts, try adding 'cache_results' => false to the array of arguments. So, this.

$your_query = get_posts(
        'posts_per_page' => -1,
        'post_type'      => 'post',
        'cache_results'  => false,

That tells WP_Query to NOT cache the results, according to this line in wp-includes/class-wp-query.php:

if ( $q['cache_results'] )
    update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);

That caching, though it can be a big help for speed if you happen to query the same post multiple times in the same request, is a memory hog and could well be the source of the memory error.

  • Note it is possible to disable adding posts to cache for other calls too, such as get_post() (which does not have such option as cache_results like get_posts()) by issuing the wp_suspend_cache_addition( true ); command before. Consider calling wp_suspend_cache_addition( false ); when done with the critical part of the code, to allow caching for other code that runs later, if any. Commented Oct 16, 2022 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.