Can someone help me figure out what is going on here? I have a post relationship field that I am using to dynamically populate two fields. I have run into an error that causes the site to freeze up, as well as my computer. (I am working on a local installation).
When I un-comment the relevant section of code and load the edit screen for the vehicles post type, that is when I get the problem. Baffling to me because the two snippets of code are almost identical.
I’ve disabled plugins, switched themes, and still the same error . . . any idea what might be causing the problem? Is it something really obvious that I’m missing? I get this when only running ACF Pro and CPT UI on twentyseventeen.
In fact, if I comment out just the following line in the first snippet, I do not have the problem:
$makes_models = get_field(‘makes_and_models’, $vid, false);
Haven’t checked error logs . . . that will be next step. No errors in my console though, and nothing when I turn on WP_DEBUG.
To follow up on this, I am getting a php error:
Fatal error: Allowed memory size of 4294967296 bytes exhausted (tried to allocate 262144 bytes) in /var/www/html2/wp-includes/functions.php on line 3583
Not sure if this will make any difference but you are using setup_postdata() but you are not calling wp_reset_postdata().
Second problem, these are filters and you must return the value of $field back to ACF, which you are not doing in either filter You should be using add_filter() rather than add_action().
Somehow, and I can’t pin it down exactly, but you are causing an infinite loop when both of these filters run at the same time. This could be due to the problems I already gave or could be from something else.
Thanks for the suggestions. You set me on the right path. I had wp_reset_postdata in there originally but I guess I deleted it when I trimmed down my code.
I can’t believe that I didn’t notice I was using add_action when I should have been using add_filter. Glad you pointed that out or I would have had even more problems. Turns out though, that isn’t actually what was at the root of my problem either.
I *think* that I’ve figured this out. I say this a bit hesitantly because I have had no end of problems with this particular item. It would appear that the problem was, having two fields with the same name referencing them. Basically even after I changed it to add_filter I was still having the same problem, but I noticed that I was able to work around the problem by using get_post_meta instead. But the data being returned was wrong.
Long story short, I opened up the field group, noticed a whole bunch of field groups, and then had the sudden “eureka” moment that two fields being referenced had the same name and might be stepping on each others’ toes. Sure enough when I update the field names so that they are not identical, things start working as they should. Or at least, so it would seem. I guess if you have a filter applied to acf/load_field/name=make then it gets applied to all fields with that name. Which makes total sense.
You can avoid the load field problem with duplicate field names by using the field key instead. On the other hand, if the fields with the same can be edited on the same post then you’ll have a problem. I will generally use the same name for fields in different field groups when those fields are used for an identical purpose.
The makes field in the “vehicle types” post type populates the available “makes” choices for the vehicle post types. So they’re not editable on the same post, but one references the other, so that’s probably why the loop was happening. Yes, I could have avoided the problem by referencing field keys instead, good point. In this case I just changed one of the field names.
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!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.