WordPress Admin: How to Fix the “Fatal Error: Allowed Memory Size…” Error

The Problem

If you’ve been working on a complex WordPress site with a lot of custom fields, perhaps with the Advanced Custom Fields (ACF) plugin – chances are you’ve come across this error when you try to access your post type in the WordPress admin:

"Fatal error: Allowed memory size of xxx bytes exhausted (tried to allocate 64 bytes) in /xxx/wp-includes/wp-db.php on line xxx"

The Cause

The cause of this problem is that WordPress tries to preload the posts’ meta saved in the postmeta table for all the posts which are currently being displayed in the admin.

This helps improve performance for smaller sites or sites which don’t use a lot of custom post meta / fields. But this is disastrous for sites which heavily rely on custom fields.

The Solution

Fortunately, WordPress provides a hook which lets us control this. Just add this into your functions.php file and watch your troubles go away!

/**
 * Disable Posts' meta from being preloaded.
 * This fixes memory problems in the WordPress Admin.
 */
function jb_pre_get_posts( WP_Query $wp_query ) {
	if ( in_array( $wp_query->get( 'post_type' ), array( 'my_post_type_1', 'my_post_type_2' ) ) ) {
		$wp_query->set( 'update_post_meta_cache', false );
	}
}

// Only do this for admin.
if ( is_admin() ) {
	add_action( 'pre_get_posts', 'jb_pre_get_posts' );
}

Explanation

This is what the code above does:

  1. It checks if it is the WordPress admin – If you want this to also happen on the front-end, you can go ahead and remove that condition
  2. It hooks on to the WordPress pre_get_posts action
  3. We only do this for certain post types. If you want this to happen for all post types, you can go ahead and remove that condition
  4. We set the update_post_meta_cache property to false, which forces WordPress to not load the post meta – which is what saves us all that memory!

—

Hope this was helpful! 🙂

23 responses

  1. Erik Avatar
    Erik

    Thanks, you’re a life saver! Glad I happend to stumble upon this post the day after you published it. This solved a problem I’ve had the past year with a site which relies heavily on ACF with repeater fields. Be sure to post this on the ACF board too 🙂

    Like

    1. junaid Avatar
      junaid

      Glad it helped! 🙂

      Like

  2. Olli Avatar
    Olli

    Thanks! This solved our problem as well. Hundreds of pages with complicated ACF structures = a memory hog.

    Like

  3. Marco Avatar
    Marco

    THANKS!
    You are a genius…

    Like

  4. Simone Avatar
    Simone

    I just stumbled upon this and think it will fix my problem. When the error occurs it always happens for the edit page i’m working on. Usually after adding too many fields. I can still view the front end page, but can no longer edit it due to the memory limit(256 right now).

    I can access the post type and , other pages in the post type (both front and back end).

    I so hope this works, I will let you know the results and spread the knowledge of it does.

    Like

  5. Bill Gant Avatar
    Bill Gant

    This was perfect. Solved a big problem for me. Thanks

    As I am a newbie to wordpress, it took me a while to figure out how to use it correctly.
    You should mention that:

    This should be done inside the theme’s functions.php file located at
    wp-content/themes/’MyThemeName”/functions.php
    I had to make it apply to all post types, which meant modifying the code to this:

    function jb_pre_get_posts( WP_Query $wp_query ) {

        $wp_query->set( 'update_post_meta_cache', false );
    

    }

    // Only do this for admin
    if ( is_admin() ) {
    add_action( ‘pre_get_posts’, ‘jb_pre_get_posts’ );
    }

    Thanks again for this. It’s a life saver

    Like

  6. Steve Marks Avatar
    Steve Marks

    THANK YOU THANK YOU THANK YOU. Been having this problem for ages and this sorted it once and for all.

    Like

  7. Max Avatar
    Max

    I’m almost crying with relief. Thank you SO much.

    Like

    1. junaid Avatar
      junaid

      Haha, I know what you mean 🙂 . Cheers!

      Like

  8. Niels Avatar
    Niels

    Awesome snippet! Already increased the server memory limit before but ran into the issue again. This solution is so much better, thanks a lot!

    Like

  9. Tom B Avatar
    Tom B

    LOL this took a lot of “search overload” to find – had memory issues with post meta on our e-comm sites for awhile and this totally resolved it. Thanks!

    Like

  10. Sergio Avatar
    Sergio

    Awesome. Thanks!

    Like

  11. Natacha Beaugeais Avatar
    Natacha Beaugeais

    O my god

    You are a lifesaver!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Like

  12. Sisko Avatar
    Sisko

    You saved my life man! ❤

    Like

  13. Carlos Avatar
    Carlos

    You’re really a lifesaver!!!
    THANKS a lot man!!!!

    Like

  14. Diego Avatar
    Diego

    Hi Junaid!
    Can you help me, please??
    I?m dummy on wordpress, php, codes, etc.
    What should i write in ‘my_post_type_1’, ‘my_post_type_2’?
    Thanks for your help!!

    Like

    1. Tochukwu Jerome Obiefule Avatar
      Tochukwu Jerome Obiefule

      You can remove that if statement to make it apply to all post types

      Like

  15. Andropedi Avatar
    Andropedi

    Great Job! Thank you for solving this situation…

    Like

  16. Andy Avatar
    Andy

    Awesome. Still saved the day even 3 years later!!

    Like

    1. Dan Avatar
      Dan

      Yep! Same here! A Great Solution to a very perplexing issue.

      Like

  17. Azario Lopez Avatar
    Azario Lopez

    I am getting this error:

    Use of undefined constant ?update_post_meta_cache? – assumed ‘?update_post_meta_cache?’ (this will throw an Error in a future version of PHP)

    Does anyone else see it? Other than that, I’m not experiencing any crashing or slow speeds.

    Like

    1. Junaid Avatar
      Junaid

      I think you may have missed the quotes around update_post_meta_cache

      Like

  18. Alexa Avatar
    Alexa

    Thanks A LOT! You’ve made my day in 2023.

    Like

Leave a Reply