Support

Account

Home Forums Bug Reports acf/save_post – wrong taxonomy field value

Solved

acf/save_post – wrong taxonomy field value

  • Hi,

    I use acf/save_post hook to get the field value after the post is saved. I set the priority to 20 in order to get the new value, not the previously saved one.

    For all the fields the value I get is correct, but for the taxonomy field I get an old value (like the priority was set to 10, but it isn’t).

    It happens only If I set load terms to true in the field settings.

  • Do you also have save terms set to true? I think it’s probably a bad idea to have both of these settings, I can’t think of any case where you’d want to load values but not save them.

    Anyway, what is happening if save terms is not true is that it’s always loading the terms set using the standard WP term metabox for your post type/taxonomy and without save terms being true the values are never updated so nothing is being changed when you save the post.

  • Yes. I’ve also set save terms to true but only load terms causes the issue.

  • I’ve done testing on this and the only time I see an issue is if I have one set to yes while the other is set to no.

    One other thing that can also cause this issue is if you have multiple taxonomy fields on the same post/page for the same taxonomy. You cannot do this and use save terms because each of the taxonomy fields will overwrite whatever was saved by the previous taxonomy field, load terms will then load whatever was saved by the last taxonomy field to be save the last time you updated the post. There is not work around for this.

  • I think you get me wrong. I’m not saying that the taxonomy term is not saved. The values are saved properly (for the custom field and for the taxonomy term as well). This is not the issue.

    I’m saying that the value of the taxonomy custom field is wrong when I try to get its value in the acf/save_post callback. When the value of the priority parameter is set to 20 then in the acf/save_post I should receive the newly set CF value, but instead of a new value, I get the previous one.

    
    add_action('acf/save_post', function ($postId) {
    
        get_field('image-field', $postId); // Returns the correct value
        get_field('taxonomy-field', $postId); // Returns the same value as if the priority parameter was set to 10
    
    }, 20 ); // Notice the priority parameter
    
  • As a test I created a field group that has 2 fields.
    The first field is a taxonomy field and named taxonomy
    The second field is a text field and named temp_field
    I then created an acf/save_post filter with the priority set to 20, here is the filter

    
    add_action('acf/save_post', 'test_tax_problem', 20);
    function test_tax_problem($post_id) {
      $tax = get_field('taxonomy', $post_id, false);
      update_post_meta($post_id, 'temp_field', $tax);
    }
    

    What it does is simply get the new value from the taxonomy field after ACF and puts the value into the text field so I can see what value is being returned for the taxonomy field at this point.

    Testing with different options I get these results

    
    Save Terms =  No, Load Terms =  No : returns new terms
    Save Terms =  No, Load Terms = Yes : RETURNS OLD TERMS
    Save Terms = Yes, Load Terms =  No : returns new terms
    Save Terms = Yes, Load Terms = Yes : returns new terms
    

    These are exactly the results I would expect to get. Let me know if you do not understand why.

  • I would expect the new value in the second test case as well.

    IMO The fact that taxonomy term is not saved shouldn’t make any difference.
    The value of the custom field is saved no matter if a taxonomy term is saved or not. So I would expect the new custom field value in the acf/save_post filter. This is a value of a custom field, not a value of a taxonomy term.

  • Saving the taxonomy terms means calling wp_set_post_terms(), see the documentation. https://codex.wordpress.org/Function_Reference/wp_set_post_terms

    Loading the taxonomy terms means calling wp_get_post_terms(), https://codex.wordpress.org/Function_Reference/wp_get_post_terms

    Load terms means that the values added using wp_set_post_terms() are retrieved. Turning off load terms means that the values from the ACF field are retrieved. If the terms are not updated then you will always get the same values back. With a taxonomy the values are saved into the acf field. Since you have load terms turned on then no, ACF does not get the values you saved in the field and every time you call get_field() ACF will get the values using wp_get_post_terms() and will ignore whatever is in the ACF field, no matter where you call it from or when you call it.

    I suppose that this can be a little confusing, but if you turn on load terms then whatever is in the ACF field is meaningless.

    If you want to get the values in this field and not the terms set using wp_set_post_terms() then I suggest that you use get_post_meta() to get the values that are in the ACF field.

  • Here is the code from the ACF Taxonomy field where the value is loaded. This function is called immediately after getting the value of the field when using get_field(). As you can see, if field[‘load_terms’] is true, the value is discarded and replaced with whatever is retrieved by wp_get_object_terms(). I have only included the important bits of this function and removed everything else.

    
    function load_value( $value, $post_id, $field ) {
      $value = acf_get_valid_terms($value, $field['taxonomy']);
      // load_terms
      if( $field['load_terms'] ) {
        // get terms
        $term_ids = wp_get_object_terms($post_id, $field['taxonomy'], array('fields' => 'ids', 'orderby' => 'none'));
        $value = $term_ids;
      }
      return $value;
    }
    
  • Thank you very much for the detailed explanation. Now I understand what happens, but IMO it shouldn’t be done this way. So it’s not a bug, but a weird, unintuitive behaviour.

    Using get_post_meta() is a good solution. I didn’t even try to use get_post_meta() because I thought that the value is not saved in the custom field. I thought it’s a bug.

    It would be good if it was documented.

  • It makes perfect sense and seems intuitive to me.

    ACF4 only had one setting, both load and save were either on or off. In ACF 5 they were separated, why? I don’t know.

    Like I said way back up at the top, what doesn’t make sense is doing one without the other and I don’t think they should be separated and I don’t know why this ability was included in ACF5. I personally cannot think of a single reason why I’d want to do one without doing the other.

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

You must be logged in to reply to this topic.