I recently had an «allowed memory size exhausted» problem and I found that it was because of page type location rule (with ACF 4.2.2 but ACF 5.3.1 does it the same way).
When checking if page is parent (with children), ACF uses get_pages() function which returns all children as WP_Post object and store them in cache (and if you have a lot of children, this could use a lot of memory).
So I changed function rule_match_page_type() to get only one child (since we just want to check if number of children is equal or greater than zero) and not the whole WP_Post object, but only the post ID :
$children = get_children(array( 'post_type' => $post->post_type, 'post_parent' => $post->ID, 'numberposts' => 1, 'fields' => 'ids', ));
And my memory problem is gone.
Hope this helps.
You can also create your own rule. Please take a look at this page: http://www.advancedcustomfields.com/resources/custom-location-rules/.
I hope this helps too.
Hi. I know I can create own rule, this is very useful and I often use it. But why create something that already exists ? Is there any reason I don’t know that explains why ACF gets all children of a page as WP_Post objects only to see if there is one child ?
Your code works well, but in my case, it filled my php memory and broke my page.
I made some more tests and in my case, your location rule code runs only once and takes around 2.5 seconds (!)… with the code above it takes around 0.0010 second.
I think it can be a good improvement for all ACF users & lovers like me.
Thank you for your consideration.
I’m sorry I thought you don’t know about this custom rule. I just wanted to let you know about it so you can custom your location rules instead of modifying the core function. This way your custom rule will still be there when you updated ACF.
Regarding why ACF gets all the children, I’m not really sure about that because I’m not the developer. For this kind of thing, please open a new ticket from http://support.advancedcustomfields.com/new-ticket so this issue ca be passed to the developer directly.
Please explain the issue again in the ticket.
Thank you very much!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2022 Advanced Custom Fields.