Home › Forums › Add-ons › Repeater Field › Taxonomy field inside a repeater not saving properly? › Reply To: Taxonomy field inside a repeater not saving properly?
This was very useful and pointed me in the right direction. I’m not an expert by any means, so forgive me If what I’m saying is mistaken. The code seems to have to small errors, that can go silently unreported and prevent admin pages from saving properly, at least that was my experience and it took me forever to realize what is going on.
Below I’m pasting the amended code, with comments on what was missing or needs to be changed in case someone else, like me, needs to use this. Thanks so much anyways because this made a difference.
add_action('acf/save_post', 'update_repeater_post_terms');
function update_repeater_post_terms($post_id) {
if (get_post_type($post_id) != 'YOUR POST TYPE HERE') {
return;
}
$terms = NULL; // this will clear terms if none found
if (have_rows('YOUR REPEATER', $post_id)) {
$terms = array();
while (have_rows('YOUR REPEATER', $post_id)) {
// SET THE ROW, WAS MISSING
the_row();
// add this term to the array
$terms[] = get_sub_field('YOUR SUB FIELD', false); // false for no formatting
}
}
// DO NOT THINK THIS IS THE RIGHT WAY, set_objects_terms is a hook
// set_object_terms($post_id, $terms, 'YOUR TAXONOMY HERE', false);
// INSTEAD WE NEED THIS SIMILARLY NAMED FUNCTION:
wp_set_object_terms($post_id, $terms, 'YOUR TAXONOMY HERE', false);
}
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.