Home › Forums › Add-ons › Repeater Field › Is there no 'simple' delete_sub_field() function?
Potentially I’m being a dumbass (!), but is there really no simple delete_sub_field() function that removes an item completely from the DB? I’ve read through some of the workarounds and custom functions, but all I want is to handle a delete_sub_field in the same way I might do an update_sub_field during a loop.
My scenario is thus:
1. My Field Group has a Repeater. Let’s call it ‘Drink’.
2. The first Repeater field is a radio field. Let’s say the options are ‘Coffee’ and ‘Tea’.
3. There are subsequent Conditional fields specific for each drink type.
Now, if the user creates everything correctly first time, everything is great. However, if the user creates a Tea drink and subsequently wants to change it to Coffee, I’m left with duff data in the DB because Conditional fields are not reset to their default when the Conditional is changed. That in itself is OK, as I have some cleanup routines that I use on acf/save_post (using a $screen = get_current_screen();
/ $type = $screen->post_type;
check) but in doing so I’ve hit upon there being no simple delete_sub_field() function.
Is the only way to ‘reset’ the correct state for the Repeater row to update the row with the correct sub-fields rather than just delete the unnecessary sub-fields?
Actually, there is a delete_sub_field() function, but it does not seem to be documented. https://support.advancedcustomfields.com/forums/topic/remove-sub_field/
There seems to be another option here https://gist.github.com/aaronjheinen/53ff4a44362927711d37
Thanks Jon. I was aware of those two solutions but I should have included them as links in my post to make it obvious. My apologies for laziness!
In my eyes, delete_sub_field
is not a valid sub-field version of delete_field
due to it setting the value to null rather than deleting the data, which over time will create a lot of empty and unnecessary custom field data.
I have coded my own workaround for now but I was surprised to find this wasn’t a core function in this otherwise super-thorough mega plugin. 😀
Your best bet for getting this changed would be to open a support ticket and explain what you’re looking for https://support.advancedcustomfields.com/new-ticket/
Good idea John (apols for previous ‘Jon’!). Ticket raised. Thanks for the responses (and your broad support and commenting across the forums).
The topic ‘Is there no 'simple' delete_sub_field() function?’ is closed to new replies.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.