Support

Account

Home Forums ACF PRO Update_field works a bit strange with new_post

Solved

Update_field works a bit strange with new_post

  • Hi ACF team!

    Big Thanks for your work & support as usual!

    I am PRO user and, I found this strange behavior this time.

    When I use Update_field(‘selector’, ‘value’) to a new post,
    ‘value’ will be set as meta_value to meta_key ‘selector’ in wp_postmeta.
    but Nothing will be set to meta_key ‘_selector’
    while I expected ‘field_(…hash…)’ would be in ‘_selector’.

    Is it by design?
    or avoidable somehow?

  • When using update_field() on a new post you must use the field_key when calling the function. It is always better to use the field_key in this function anyway. See the information about using the field key in the documentation https://www.advancedcustomfields.com/resources/update_field/

  • Hi @hube2

    Thanks for ur advise always!
    I should have read the doc well…

    Then I tried this code below

    $my_post = […];
    $post_id = wp_insert_post( $my_post );
    $field = get_field_object(‘selector’, $post_id );
    Update_field($field[‘key’], ‘value’, $post_id);

    but $field is empty, so Update_field() does not work.

    Do I have to use SQL instead of get_field_object()
    to get field_key from field_name in case of a new post?

  • 
    $field = get_field_object('selector', $post_id );
    

    is empty for the same reason that update_field() is not updating properly. The field does not already exist for the post so it returns nothing. Basically, anywhere that you’re coding the field name into your code you need to use the field key instead.

  • Hi @hube2

    Thanks aganin!

    In the end, I made this sa a solution.
    (very rough code though…)

    
    function my_get_field_object_for_new_post($selector, $parent_selector = ''){
    
    	global $wpdb;
    	$where = '';
    	$parent_id = false;
    	$result_key = false;
    
    	if($parent_selector != ''){
    		$sql = "SELECT ID FROM $wpdb->posts WHERE post_excerpt = %s AND post_type = 'acf-field'";
    		$sql = $wpdb->prepare($sql, $parent_selector);
    		$result = $wpdb->get_row( $sql );
    
    		if((int)$result->ID > 0){
    			$parent_id = $result->ID;
    		}
    		else {
    			$sql = "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'acf-field-group'";// can be group name
    			$sql = $wpdb->prepare($sql, $parent_selector);
    			$result = $wpdb->get_row( $sql );
    			if((int)$result->ID > 0){
    				$parent_id = $result->ID;
    			}
    		}
    		if($parent_id){
    			$where = " AND <code>post_parent</code> = " . $parent_id;
    		}
    
    	}
    
    	if($selector!=''){
    		$sql = "SELECT post_name FROM $wpdb->posts WHERE post_excerpt = %s AND post_type = 'acf-field'".$where;
    		$sql = $wpdb->prepare($sql, $selector);
    		$result = $wpdb->get_row( $sql );
    		if( $result->post_name ){
    			$result_key = $result->post_name;
    		}
    	}
    
    	return $result_key;
    
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.