Junaid Bhura a web developer in Melbourne, Australia - specializing in WordPress development.

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 to “WordPress Admin: How to Fix the “Fatal Error: Allowed Memory Size…” Error”

  1. 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 🙂

  2. 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.

  3. 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:

    1. This should be done inside the theme’s functions.php file located at
    wp-content/themes/’MyThemeName”/functions.php

    2. 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

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

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

  6. 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!

  7. 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!!

  8. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *