Support

Account

Home Forums General Issues Need help with save_post hook

Solved

Need help with save_post hook

  • Hi,

    I love ACF (especially now that I found out it can do category meta as well!) but am running into a problem now.
    I’m trying to do the following: I have 3 custom fields, “address”, “postal code” and “city” (along with some others). Upon saving a post with these fields I want to combine these fields, send it to an external API (address to coordinates) and save the result in a new custom field.
    I can hook save_post (or acf/save_post) and print out $_POST[‘fields’] which contain the new values of the custom fields. However, the problem is these names change! When I tried it earlier, one of the fields was called field_51f67d7586013 but now it’s field_51f67f81d00d6.
    Can I somehow retrieve which POST field belongs to which custom field, i.e. field_51f67d7586013 is ‘address’ ?

  • Okay, I was mistaken. I was editing a different post type, oops… Turns out the field names don’t change after all. Still, my question still stands 🙂

  • Hi @Spinal

    The easiest way is to use the get_field function to load any data (even in the save_post action!)

    Make sure you run your code AFTER ACF has saved, like so:

    
    <?php 
    
    add_action('acf/save_post', 'function_name', 20);
    
    function function_name( $post_id )
    {
    	$new_val = get_field('field_name', $post_id);
    }
    
     ?>
    
  • Thanks for your answer. I was aware of that functionality, I guess I was thinking too hard about it and trying to find another way. In the end, this seems like the easiest and best way to do it 🙂

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

The topic ‘Need help with save_post hook’ is closed to new replies.