WordPress

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

By September 23, 2016 10 Comments

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

10 Comments

  • Erik says:

    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 🙂

  • Olli says:

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

  • Marco says:

    THANKS!
    You are a genius…

  • Simone says:

    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.

  • Bill Gant says:

    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

  • Steve Marks says:

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

  • Max says:

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

  • Niels says:

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

Leave a Reply