Support

Account

Home Forums Feedback ACF/save_post question Reply To: ACF/save_post question

  • ah well..

    If we look at the code logically.. you input the $_POST into the $fields variable.. then change the value of one of the array keys in $fields, but $_POST is never really altered.. $fields != $_POST if you see what I mean 🙂

    So your code works in a sense but it is not affecting the actual values being saved.

    I think this should work:

    
    function my_acf_save_post( $post_id )
    {
    	// vars
    	$fields = false;
     
    	// load from post
    	if( isset($_POST['fields']) )
    	{
    		$fields = $_POST['fields'];
    		
    		// concert details
    		
    		// event start date
    		//$fields['field_5217ac66687dc']
    		// event end date
    		//$fields['field_524b4ca5ff418']
    		
    		if( empty( $fields['field_524b4ca5ff418'] ) )
    			$_POST['fields']['field_524b4ca5ff418'] = $fields['field_5217ac66687dc'];
    		
    		// calendar details
    		
    		// event start date
    		//$fields['field_524b4c06af2a2']
    		// event end date
    		//$fields['field_524b4c3aaf2a4']
    		if( empty( $fields['field_524b4c3aaf2a4'] ) )
    			$_POST['fields']['field_524b4c3aaf2a4'] = $fields['field_524b4c06af2a2'];
    	}
     
    	// ...
    }
     
    // run before ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);
    
    

    I suppose you could clean that up a bit if you like and I’m not 100% sure if I wrote the $_POST[‘field’][‘fieldkey’] correctly since I havent looked at the arrays myself but all in all it should set you on the right path!