Support

Account

Home Forums General Issues Check a taxonomy radio field in ACF Save Post

Solving

Check a taxonomy radio field in ACF Save Post

  • Hello,
    In my ACF save post hook:
    I need to check a taxonomy radio field programmatically depending on the checked value from another taxonomy radio field.
    Thanks for help

  • It depends on if you’re checking before or after ACF has saved the values.

    Before, your action has a priority < 10, you need to field key and the value submitted in the other field will be in $_POST[‘acf’][‘THE FIELD KEY’].

    After, your action has a priority > 10, you can just get the other field like you normally would with get_field()

  • thanks John, my action has 20 as priority. I can check after save the value or before, nevermind.
    But in another post you said to use wp_set_object_terms( $post_id, $term_id, ‘name’);
    isn’t ?
    thanks

  • I’m not exactly sure what you’re referring to, I answer too many questions here to keep track 😛

    Anyway, the value of the field you’re checking has nothing to do with the value of the other field unless you make it.

    What exactly are you trying to do? Are you trying to validate one field based on another field? If this is the case then what you want to do is use an acf/validate_value filter https://www.advancedcustomfields.com/resources/acf-validate_value/

    You can look at other values or other fields by looking at $_POST['acf']['KEY OF OTHER FIELD']

  • Actually i have 2 ACF taxonomy radio fields in a form in ACF save post.
    First is visible, second is hidden.

    I would like to get the value (ID) of the first taxonomy field checked by user.

    Depending on this value, i want to set the value of the second field and save it to the database.

    i tought i had to use get_terms and wp_set_object_terms

  • That depends on several things.

    1) Are these taxonomy fields in a repeater?
    2) Is your ACF field you want to update set up to “Load Terms” and “Save Terms”?
    3) Do you want to save the values in just the ACF field or do you want to Save/Add these values Terms for the post in WP?

  • 1) Are these taxonomy fields in a repeater?
    NO
    2) Is your ACF field you want to update set up to “Load Terms” and “Save Terms”?
    Not sure , YES i suppose
    3) Do you want to save the values in just the ACF field or do you want to Save/Add these values Terms for the post in WP?
    For the POST in WP

  • Can you give me a link to this other question I answered?

    At this point I can only say what I would do.

    When adding the taxonomy field in ACF there are settings for saving and loading terms, I would turn both of these on.

    
    function my_acf_save_post_function($post_id) {
      // get the value of the first taxonomy field
      // I am getting it unformulated
      // which means that it's returning an array of term IDs
      // you might do this differently depending on what you want to do
      get_field('other_tax_field', $post_id, false);
      // check this field for whatever condition you are looking for
      if ($condition) {
        // get the value of the field you want to change
        $other_field = get_field('the other field', $post_id, false);
        // append a term ID value to the value
        // if it does not already exist
        if (!in_array($new_term_id, $other_field)) {
          $other_field[] = $new_term_id;
        }
        // update acf field
        // acf will take care or updating terms because of the settings
        update_field('the other field', $other_field, $post_id);
        
      } 
    }
    
  • thanks i am going to try it.
    this the related post
    https://support.advancedcustomfields.com/forums/topic/update_field-for-taxonomy-field-type/

  • That was an old question, more than 2 years ago, and I was’t the one that answered it. I don’t know if ACF handled the update differently then, I don’t remember, but with the save/load terms settings I think that ACF should update the post terms…. I could be wrong, I have been wrong before. Please let me know if it does not work.

  • thanks, assuming i know the taxonomy ids , could it be like this ?

    function my_acf_save_post_function($post_id) {
    
    $condition = get_field('first_tax_field', $post_id, false);
    // get the value checked by user
    
    if ($condition=='36') {
    
        update_field('second_tax_field', '78', $post_id);
    
    }
    else {
        update_field('second_tax_field', '79', $post_id);
    
    }
    
    }
  • In my taxonomy ‘from’ i have only 2 values, ID are 68 and 69.
    So update_field(‘from’, ’69’, $post_id);
    should automatically set the from value isn’t ? because it does not.

    Actually I have removed the taxonomy radio field because i don’t want the user can choose it. So can i directly update the taxonomy value when ACF save post ?
    thanks

    thanks for help

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.

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.