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:
- 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
- It hooks on to the WordPress pre_get_posts action
- 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
- 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”
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 🙂
Glad it helped! 🙂
Thanks! This solved our problem as well. Hundreds of pages with complicated ACF structures = a memory hog.
THANKS!
You are a genius…
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.
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
THANK YOU THANK YOU THANK YOU. Been having this problem for ages and this sorted it once and for all.
I’m almost crying with relief. Thank you SO much.
Haha, I know what you mean 🙂 . Cheers!
Awesome snippet! Already increased the server memory limit before but ran into the issue again. This solution is so much better, thanks a lot!
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!
Awesome. Thanks!
O my god
You are a lifesaver!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
You saved my life man! <3
You’re really a lifesaver!!!
THANKS a lot man!!!!
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!!
You can remove that if statement to make it apply to all post types
Great Job! Thank you for solving this situation…
Awesome. Still saved the day even 3 years later!!
Yep! Same here! A Great Solution to a very perplexing issue.
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.
I think you may have missed the quotes around
update_post_meta_cache
Thanks A LOT! You’ve made my day in 2023.