Support

Account

Home Forums Feedback ACF/save_post question

Solved

ACF/save_post question

  • I’m not sure where to ask questions….

    I read this http://www.advancedcustomfields.com/resources/actions/acfsave_post/

    And I understand it, but whne I output the $_POST I get an array of field names like this “field_524b4c06af2a2”.

    What I have is two custom date fields.

    event_start_date and event_end_date

    when saving if the event_end_date is empty I want to save the value of event_end_date to be event_start_date.

    the reason is I’d prefer not to have two required date fields, but to query a date range I need two days.

    If someone could help me out.

    Thanks.

  • Hi!

    I believe the field_524b4c06af2a2 is the field key for one of your fields. you can open up the panel settings when on the edit page of the field group and check to show field keys and you’ll see which ones you should work with.

  • Jonathan, thanks for the response. I guess I just need to manipulate the field keys then and ACF does all the heavy lifting in the back end then.

    I was just worried that the field keys were dynamic and would change. That’s why I was looking for the actual key names created such as mom_event_date”

    I’ll give that a try and see how it works.

  • Np Kyle,

    you wont have to worry about the field keys tho, they’re actually the ones you really should use since they will never change and are 100% unique compared to field names which depends on the developers paranoia 😉 (I myself always write custom fields like “acf_reference_name” since I want to be 500% sure it would never collide).

  • K, sorry, I must be missing somehting. Here’s my code that doesn’t seem to 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'] ) )
    			$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'] ) )
    			$fields['field_524b4c3aaf2a4'] = $fields['field_524b4c06af2a2'];
    	}
     
    	// ...
    }
     
    // run before ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);
  • are you sure the fields are even set?

    You could do some debugging:

    
    print_r($fields);
    die();
    
    

    put that after you set the $fields variable. when you update a post it should then just send you to a white page within the admin with the $fields variable printed.

  • Did that already. It prints out an array of fields. That’s actually how I got the field keys.

    Maybe I’m wrong about what the hook does and how it does it. Is the $fields array saved, or is the $_POST array saved.

    All I know is when I hit update, the end date field is still blank.

  • 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!

  • Sadly that didn’t work.

    I checked the original method inside the acf.php file and tried this as well with no results.

    function my_acf_save_post( $post_id )
    	{
    
    		// load from post
    		if( !isset($_POST['fields']) )
    		{
    			return false;
    		}
    		
    
    		// loop through and save
    		if( $_POST['fields'] )
    		{
    			
    			$fields = $_POST['fields'];
    		
    			// concert details
    			
    			// event start date
    			//$fields['field_5217ac66687dc']
    			// event end date
    			//$fields['field_524b4ca5ff418']
    			
    			if( empty( $_POST['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( $_POST['fields']['field_524b4c3aaf2a4'] ) )
    				$_POST['fields']['field_524b4c3aaf2a4'] = $fields['field_524b4c06af2a2'];
    			
    			
    			
    			foreach( $_POST['fields'] as $key => $value )
    			{
    				// parse types
    				// - caused issues with saving numbers (0 were removed)
    				//$value = apply_filters('acf/parse_types', $value);
    		
    				// get field
    				$field = apply_filters('acf/load_field', false, $key );
    				
    				// update field
    				do_action('acf/update_value', $value, $post_id, $field );
    				
    			}
    			// foreach($fields as $key => $value)
    		}
    		// if($fields)
    		
    		
    		return true;
    	}

    I’m super stumped, which sucks sine I write code all day long.

  • If anyone can solve this for me I’d love to buy them a few beers.

    I’m not sure if it makes any difference or not that its a date picker field.

  • Got it to work using this code here:

    function my_acf_load_field( $value, $post_id, $field  )
    {	
    	$event_start = get_field_object('field_5217ac66687dc', $post_id);
    	$event_end = get_field_object('field_524b4ca5ff418', $post_id);
        
    	if( empty( $event_end['value'] )):
    		$value = $event_start['value'];
    	endif;
    
    	return $value;
    }
    
    //add_filter('acf/update_value/key=field_524b4ca5ff418', 'my_acf_load_field', 10, 3);

    Problem is, if you save it duplicates it if empt, then if you clear it and hit save again, it is blank till you hit save a second time.

  • Odd that your previous function didn’t work..

    I tried thinking outside of what you’ve done so far and perhaps this would work? It’d be a lot simpler altho maybe not as good performance-wise.. I don’t think it matters tho since it’s in the admin and it’s not that big off a loss that anyone would notice.

    
    function my_acf_save_post( $post_id )
    {
    
    	//The fields have already been saved to the database so lets retrieve them
    	$event_start = get_field('field_5217ac66687dc', $post_id);
    	$event_end = get_field('field_524b4ca5ff418', $post_id);
    	
    	//check if event_end exists, if it was empty it should not. You can make sure of this in database.. perhaps it has to be changed to $event_end == ''
    	if(!$event_end){
    		//update event_end. Parameters are field key of event_end, event_start value and the post id. 
    		update_field('field_524b4ca5ff418', $event_start, $post_id);
    	}
    	
    }
     
    // run AFTER ACF saves the $_POST['fields'] data to database
    add_action('acf/save_post', 'my_acf_save_post', 20);
    
    
  • Jonathon,

    Thanks so much for the help. I ended up just making the end date mandatory. This turned into to much work so I just went that route.

    I’ll try your last suggestion still, but if I don’t use it I still wanted to just say thanks.

    Send me your paypal email and I’ll buy you a beer.

  • Haha that’s true.. kind of making a mountain out of a molehill.. 😉 But still, overcoming issues like these is what makes this job fun!

    Yeah I suggest you try my last solution, according to my brains logic it should work in it’s simplicity!

    Unfortunately I don’t have a paypal account since I rarely use paypal in my private life and usually just get the glory and appreciation of helping out!
    But I don’t suppose you’ll be at the wordcamp europe this weekend?

  • I only wish I could afford to buy a ticket from Vancouver BC Canada to WordCamp Europe. 😛

    Sadly it’s not in this years budget.

  • Ah I see 🙂

    We’ll then I’ll settle for the glory ey! ;P

    • jtyarks

    • January 19, 2015 at 10:12 pm

    I know this post is over a year old now, but I recently had a similar problem to kylerumble and wanted to provide a solution on the off-chance that someone else can use it.

    The problem: A company takes part in several events, some of which span multiple days, and wants to highlight them across two sections on their website: Upcoming and Past. The Past section shows all events from the previous year up until (the relative) yesterday with the exception of multi-day events. If an event spans multiple days, it continues to be shown in the Upcoming section until the current date is after the last day of the event.

    Like kylerumble, I didn’t want the editors to have to fill in the date twice if the event only lasts a single day.

    The solution:

    I made three ACF fields – “Multi-day Event” (True / False field), “Start Date,” and “End Date” (both Date Picker fields) – and set a conditional around the Start Date field so it only displays if the “Multi-day Event” field is checked.

    Using Jonathan’s last function as a starting point, I used the save_post action to update the field when necessary:

    // run AFTER ACF saves the $_POST['fields'] data to database
    add_action('acf/save_post', 'single_day_event', 20);
    
    function single_day_event( $post_id ) {
        // Ensure we have ACF fields on the post.
        if( empty($_POST['acf']) ) {
            return;
        }
        
        // Set up our variables with the respective ACF field keys
        $multi_day_event = $_POST['acf']['field_123'];
        $start_date_field = $_POST['acf']['field_456'];
        $end_date_field = $_POST['acf']['field_789'];
    
        /* Update the Start Date field value with the End Date field value if any of the following are true:
        1. The Start Date field is empty
        2a. Multi-day Event is empty and the Start Date doesn't equal the End Date
        2b. Multi-day Event's value is 0 and the Start Date doesn't equal the End Date (This is in case an event is ever set to Multi-day and then later changed to a single day)
        */
        if( ( $start_date_field == '' ) || ( ( $multi_day_event == '' || $multi_day_event == 0 ) && ( $start_date_field != $end_date_field ) ) ){
    		update_field('field_456', $end_date_field, $post_id);
    	}
    	
    }

    Cheers!

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.