How can I track and output when a field is updated? (currently using Advanced Custom Fields)

Trying to work out how I could track the time that a field was updated and display that time on a page.

So far:

Read More

Using advanced custom fields and the options page the user updates some fields

What I’d like to be able to do

Capture the time this happens and output it onto a page

Why?

Each day the user is inputting prices (which effect all prices across the site) and would like a line that reads “prices correct as of …”

In Summary

So to sum it up, is it possible to track when this custom field is updated? Or any other ideas for tracking this event? I know I could put an extra field underneath where the user could input todays date and time but I’m trying to set up an automated failsafe system for them.

I don’t have to use advanced custom fields and I’m welcome to other ideas, but that’s what is set up currently so I was looking to integrate into it.

Related posts

Leave a Reply

1 comment

  1. You can use the acf_update_value filter to set a post meta value whenever the field is updated.

    Here’s an example for a field named test_field. Check if the current field is test_field, then get its old value and compare it to the new value. If it has been updated, update a post meta field named _update_time with the current time. In your template, use get_post_meta to output the time.

    function wpa85599_acf_update_value( $value, $field, $post_id ){
        $old_val = get_field( 'test_field', $post_id );
        if( $old_val != $value )
            update_post_meta( $post_id, '_update_time', time() );
    
        return $value;
    }
    add_filter( 'acf_update_value-test_field', 'wpa85599_acf_update_value', 10, 3 );
    

    Have a look through the ACF documentation for other filters and actions. There are also more general filters that run on all fields or just certain field types, as welll as this filter which is specific to a single field name.

    EDIT-

    I just noticed you said options page, not a post. To make this work with the options page, just change update_post_meta to instead update an option:

    update_option( 'update_time', time() );