Support

Account

Home Forums Front-end Issues More issues with update_field($field_name) not retrieving value

Solving

More issues with update_field($field_name) not retrieving value

  • This seems to be a reoccurring issue for myself and others: understanding when it is necessary to use ‘field_value’, or when we can use the more versatile ‘field_name’. I have a feeling that is in this case, it is a matter of ACF not being fully loaded when my function is called.

    In functions.php, I have 2 functions:

    1. do_something() processes data and updates fields when the post is saved in WP Admin, thanks to the acf/save_post action.

    function do_something( $post_id ) {
    
    	$field_1 = get_field('field_1', $post_id);
    	$field_2 = get_field('field_2', $post_id);
    
    	// do stuff
    
    	update_field( 'field_3', 'value', $post_id );
    	
    }
    
    add_action( 'acf/save_post', __NAMESPACE__ . '\\do_something', 10 );

    2. update_post_data() is called when the post is loaded in the front-end, checks if a certain amount of time has passed since the post was last updated, and calls do_something() to update the data.

    function update_post_data() {
    
        global $post;    
        $post_id = $post->ID;
        $update_interval = 1800; // 30 minutes
        $last_update = get_post_modified_time( 'U', true, $post_id );
        $time = date('U');
        if ($time - $last_update > $update_interval) {
            do_something($post_id);
        }
    
    }
    
    add_filter( 'the_post', __NAMESPACE__ . '\\update_post_data', 10 );

    do_something() works perfectly when I initially create the post (acf/save_post). However, when it is called from inside update_post_data() it only works with field keys. I’d like to use field names to ensure compatibility.

    PS: An other thing, if anyone knows how I can update post_modified_time in update_post_data(), that would be awesome.

  • According to this document http://www.advancedcustomfields.com/resources/update_field/

    So when can I use $field_key?

    You can and should use the $field_key 100% of the time.

    The problem with using $field_name is that if the reference does not already exist, ACF will not be able to find the field object and will not be able to save the value. This situation would occur if you had used code to insert a post.

    Also, it is more efficient to use the field_key as the first parameter in the update_field function as it bypasses the reference look up.

    I know that you’d like to use the field name, but it is better to always use the field key when updating fields to avoid the problems that you’re seeing.

  • I read that. The field name does exist however, since when I call the function the second time, the fields have already been created.

    This causes me to wonder how one could properly write a plugin for deployment having to rely on field keys which may vary from one installation to an other.

  • It could be that ACf is unable to match up the field name to the field key, I can’t say.

    What version of ACF are you using.

  • I was just thinking about your question

    This causes me to wonder how one could properly write a plugin for deployment having to rely on field keys which may vary from one installation to an other.

    If I understand this right, you are creating a plugin but you’re using the editor to create the fields each time you use the plugin?

    Why not instead export the fields as a JSON file and in your plugin use that file to create an identical field group, that way the field keys would be reliable. Even if this is for ACF4, you can register the field group with PHP with the same effect.

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

The topic ‘More issues with update_field($field_name) not retrieving value’ is closed to new replies.