Support

Account

Home Forums ACF PRO How to create a calculated ACF field (saved in the DB)

Solved

How to create a calculated ACF field (saved in the DB)

  • HI all,

    I need to create an ACF field that is ‘calculated’ (based on the values of other ACF fields) and saved in the DB.

    This field should NOT be visible in the backend edit form (as it is calculated user should not edit the same!).

    Thanks!

  • use a hidden field and update value of it based on the other values.

    may use that: https://github.com/folbert/acf-hidden (out of this thread)

    than use update_field or update_value to save it into DB

  • Hi thank you for the suggestion.

    The issue is that on the php side while I’am saving the new value I need to get the values of other ACF fields, something like

    $total = get_field(‘price’) * get_field(‘qty’);
    update_field(‘total’, $total);

    BUT the issue is that I cannot use get_field(‘xxxx’) as the field xxx is not yet saved in the DB (as is a new post).

  • what about using this: wp_update_post( $my_post );

    • inside update_value filter?
    • inside save_post action?

    i use them to update post_title or post_content with values that come from get_field

    or the save action: (all you need is a hidden field with name “totalprice“)

    function my_acf_update_totalprice($post_id)
    {
        $total = get_field('price') * get_field('qty');
        $value = $total;
        $field_name = "totalprice";
        update_field($field_name, $value, $post_id);
    }
    add_action('save_post', 'my_acf_update_totalprice');
  • Yes this I think can work. I was looking for some action from ACF instead is correct you use ‘save_post’ action of WP. So once the post is saved, ACF get_field() function will work !

    Thanks 🙂
    Angelo

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

The topic ‘How to create a calculated ACF field (saved in the DB)’ is closed to new replies.