Home › Forums › Backend Issues (wp-admin) › Updating Linked Taxonomy on Save
This is related to http://support.advancedcustomfields.com/forums/topic/conditional-logic-not-clearing-unused-fields/
So now that the field hidden by conditional logic is empty, when the post save occurs, the appropriate ACF meta_key is rendered empty. The problem is that on save, it is not correctly unlinking the Taxonomy from the Post in the wp_terms_relationship table.
It DOES correctly save if the taxonomy multi-select field is emptied by switching away from it (causing it to be cleared by the call in acf_filters.js), switching back to the now-empty field, then saving the post. This results in the Taxonomy being uncoupled from the post ID in the wp_terms_relationship table.
The desired functionality is that ANY empty field hidden by conditional logic that is a taxonomy field correctly updates any linked taxonomies in the post as well.
I’d rather not have to write a custom save_post hook if I can avoid it, but that’s the direction I’m currently leaning in.
It’s a good thing your leaning that way. Fields that are hidden by conditional logic are not included in the $_POST values. I think they were at one time, but it has not been that way for a while I think. Since the hidden values are not submitted they don’t get updated.
So I’ve come down to this essentially.
When updating an ACF taxonomy field with
update_field( "field_ID", "", $post_id) in order to clear the field, it does NOT remove the taxonomy association in the
This is with the Field Type set to Taxonomy, and the following checked:
Save Terms: Yes
Load Terms: Yes
Is there a way to unset the taxonomy field association on
update_field? Currently I know what field I want to update and the applicable taxonomy, so I can use
wp_delete_object_term_relationship against the given
$post_id, but it would be nice if
update_field took care of this for me.
After giving this some though, I can’t say. I’m thinking that what you’ll need is create an acf/save_post action with a priority of 20 so it runs after the acf save. http://www.advancedcustomfields.com/resources/acfsave_post/. The reason being that since the field is hidden with conditional logic and not submitted that update field action for the field will never be called, so you can’t go that route.
In the save post hook you need to get the conditional field and test it then delete delete the content of the other fields depending on that value. You’ll need to remove the values from the acf field as well as deleting the post terms, I think.
My final solution ended up being as you suggested, and I have it fire on save_post. Basically when the post saves, it checks for the state of the two conditional objects. Depending on what’s set for those two conditionals, it does a check against each of the fields that could have data in them, and if it’s present, clears the data from that ACF Field, and then deletes the object term relationship via WordPress directly.
It appears to be working fine so far!
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.