Home › Forums › Backend Issues (wp-admin) › Use ACF/Save Post Action to update a Custom Taxonomy
Hi
A brief explanation; I have a post type called products, with a taxonomy which is product type. I have created a post type check box field with conditional logic so when the user checks the type, certain fields become available. I would use a taxonomy field, but ACF, as of yet, dose not support conditional logic on this field type. I believe the work around is to use the acf/save post action to update the taxonomy. On searching this forum, I found a bit of code which I believe will do the trick. I have modified it to work with my site, but it is not working.
function change_post_taxonomy_44582( $post_id ) {
// bail if no ACF data
if ( empty($_POST['acf']) ) {
return;
}
// get term id from $post_id (only 1 value is allowed, so it returns 1 value only)
$stored_cat = wp_get_post_terms($post_id, 'asproducts_cats');
// get submitted value from acf form
$posted_cat = $_POST['acf']['field_59154e83efb0e'];
// get term_id for the submitted value
$term_id = get_term_by( 'name', $posted_cat, 'asproducts_cats' );
// if stored value is not equal to posted value, then update terms
if ( $stored_cat != $posted_cat ) {
wp_set_object_terms( $post_id, $term_id->term_id, 'asproducts_cats' );
}
}
add_action('acf/save_post', 'change_post_taxonomy_44582', 20);
For the life of me I can not spot what is wrong, is anybody able to help and point me in the right direction.
Many Thanks
Keith
@mrkeithy did you ever figure this one out? exactly what i’m looking to do. thank you!
yes I did, here is the code I am using.
function change_post_taxonomy_44582( $post_id ) {
if ( empty($_POST['acf']) ) {
return;
}
// get term id from $post_id (only 1 value is allowed, so it returns 1 value only)
$stored_role = wp_get_post_terms($post_id, 'asproducts_cats');
// get submitted value from acf form
$posted_roles = $_POST['acf']['field_59154e83efb0e'];
// get term_id for the submitted value
$term_id = get_term_by( 'name', $posted_roles, 'asproducts_cats' );
// if stored value is not equal to posted value, then update terms
if ( $stored_role[0] != $posted_roles ) {
wp_set_object_terms( $post_id, $term_id->term_id, 'asproducts_cats' );
}
}
add_action('acf/save_post', 'change_post_taxonomy_44582', 20);
Also have a look here https://support.advancedcustomfields.com/forums/topic/conditional-logic-using-taxonomy-field/page/4/
A nice chap helped me out with this. Shout if you are still having issues
Keith
hi @mrkeithy,
Would you please take a look at this topic please?
https://support.advancedcustomfields.com/forums/topic/update-taxonomy-of-posts-selected-by-acf-relation-field/#post-123227
i’m not sure what i’m missing i appreciate it if you could give me a hint.
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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc
— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.