Support

Account

Home Forums General Issues update_field not working unless die() in function

Solving

update_field not working unless die() in function

  • HELP!!!!
    I have been really having a hard time trying to get the update_field to work. It only seems to work when I use a die(); after.
    This code works:

    function nbpe_build_address_string($post){
    	$post_id = $post->ID;
    	$value1 = 123456;
    	$field_name = 'short_description';
    	
    	update_field( $field_name, $value1, $post_id );
    	die();
    }

    But this code does not:

    function nbpe_build_address_string($post){
    	$post_id = $post->ID;
    	$value1 = 123456;
    	$field_name = 'short_description';
    	
    	update_field( $field_name, $value1, $post_id );
    }

    Please help, this is driving me nuts!
    Thanks,
    Gavin

  • From where are you calling this function (nbpe_build_address_string)?

  • I have changed the code around, but here is the entire plugin, again it will ONLY save the update_field when I have the die(); that is show in this.

    function nbpe_post_unpublished( $new_status, $old_status, $post ) {
        if ($new_status != 'trash'){ // If item is being deleted, don't do anything.
    		if ($post->post_type == 'project'){ // Make sure this is a "project" post, not a regular post.
    			if ( $old_status != 'publish'  &&  $new_status == 'publish') { // A function to perform actions when a post status changes from any non-published to published status.
    					do_action( 'save_post', $post->ID, $post, true );
    					nbpe_build_address_string($post);
    			}
    		}
    	}
    }
    add_action( 'transition_post_status', 'nbpe_post_unpublished', 10, 3 );
    
    function nbpe_build_address_string($post){
    	$post_id = $post->ID;
    	$street_number = get_field('street_number');
    	$street_direction = get_field('street_direction');
    	if ($street_direction == 'NONE'){
    		$street_direction = '';
    	}
    	else{
    		$street_direction = $street_direction.'+';
    	}
    	$street_name = str_replace(' ', '+',get_field('street_name'));
    	$street_suffix = get_field('street_suffix');
    	if ($street_suffix == 'NONE'){
    		$street_suffix ='';
    	}
    	else{
    		$street_suffix = $street_suffix.'+';
    	}
    	$city = get_field('city');
    	$state = get_field('state');
    	$zip_code = get_field('zip_code');
    	$address_space = '+';
    	$address_string = $street_number.$address_space.$street_direction.$street_name.$address_space.$street_suffix.$zip_code;
    	geoencode($address_string, $post_id);
    }
    
    function geoencode($address_string, $post_id){
    	$url_string = 'https://maps.googleapis.com/maps/api/geocode/xml?address='.$address_string;
    	$xmlStr = file_get_contents($url_string); //Read the XML returned by Google maps API into a string.
    	$xmlObj = simplexml_load_string($xmlStr); //Interprets a string of XML into an object.
    	$arrXml = objectsIntoArray($xmlObj); // Converts to an array.
    	
    	$status = $arrXml['status'];
    			
    		$latitude = $arrXml["result"]["geometry"]["location"]["lat"];
    		$longitude = $arrXml["result"]["geometry"]["location"]["lng"];
    	
    		echo 'Status: '.$status.'<br />';
    		echo 'Lat: '.$latitude.'<br />';
    		echo 'Lng: '.$longitude.'<br />';
    	
    		update_field('latitude', $latitude, $post_id);
    		update_field('longitude', $longitude, $post_id);
    	
    	die('<a href=edit.php?post_type=project><h1>Continue</h1></a>');
    }
    
    function objectsIntoArray($arrObjData, $arrSkipIndices = array())
    {
        $arrData = array();
        
        // if input is object, convert into array
        if (is_object($arrObjData)) {
            $arrObjData = get_object_vars($arrObjData);
        }
        
        if (is_array($arrObjData)) {
            foreach ($arrObjData as $index => $value) {
                if (is_object($value) || is_array($value)) {
                    $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
                }
                if (in_array($index, $arrSkipIndices)) {
                    continue;
                }
                $arrData[$index] = $value;
            }
        }
        return $arrData;
    }
  • I would rather use the acf/save_post action (and not transition_post_status) as described here: http://www.advancedcustomfields.com/resources/acfsave_post/

    This should solve your issue. If the die() is acting like this, it is because there is normally others actions taking place after the transition_post_status action, so when you script dies, you are only cancelling what is following (and what is following is putting back your data to the original posted values).

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

The topic ‘update_field not working unless die() in function’ is closed to new replies.