Support

Account

Home Forums General Issues Need help with save_post hook

Solved

Need help with save_post hook

    • Spinal

    • August 27, 2013 at 9:29 pm

    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’ ?

    • Spinal

    • August 27, 2013 at 9:37 pm

    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 🙂

    • Elliot

    • August 28, 2013 at 2:19 pm

    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);
    }
    
     ?>
    
    • Spinal

    • August 29, 2013 at 5:35 pm

    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)

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.