Support

Account

Home Forums Front-end Issues Struggling with saving ACF data on new post

Solved

Struggling with saving ACF data on new post

  • Okay, I’ve been fighting with this for a while and realized I need help.

    I have a function that creates a new post using AJAX from a front end form. That all works fine, I can even get the ACF data to save in the back end. So when I save the post I go see look at it and all the data is there.

    However when I go to the page that should have that data it’s not there, only the basic post fields are. Now stranger is if I open that post in the back end and just click update, nothing else, everything works just fine.

    Also very oddly last_name doesn’t work at all, that is the correct field name, it just doesn’t return a key.

    Any idea what I am doing wrong?

    Point of note: $city = get_field_object(‘city’); and then $city[‘key’] returns field_city, not the normal field_some_hash that I see everyone else posting. Why is that?

    function save_pledge_post($data) {
    
    	$new_post = array(
    		'post_title' =>$data['name'],
    		 'post_status' => 'publish',
    		 'post_author' => 1,
    		 'post_type' => 'pledgers'
    	);
    
    	$post_id = wp_insert_post($new_post);
    
    	$last_name = get_field_object('last_name');
    	$city = get_field_object('city');
    	$postal_code = get_field_object('postal_code');
    	$email = get_field_object('email');
    	$latitude = get_field_object('latitude');
    	$longitude = get_field_object('longitude');
    	$image = get_field_object('image');
    	$pldege_type = get_field_object('pldege_type');
    
    	update_field($last_name['key'], $data['last_name'], $post_id);
    	update_field($city['key'], $data['city'], $post_id);
    	update_field($postal_code['key'], $data['postal_code'], $post_id);
    	update_field($email['key'], $data['email'], $post_id);
    	update_field($latitude['key'], $data['latitude'], $post_id);
    	update_field($longitude['key'], $data['longitude'], $post_id);
    	update_field($image['key'], $data['image'], $post_id);
    	update_field($pldege_type['key'], $data['pldegeType'], $post_id);
    
    	return $post_id;
    
    }
  • So, there are tons of Google results on this and I finally found one that actually works.

    Trying to get the field keys programatically is a waste.

    Simply go into the Custom Fields back end, scroll down the field options, check the Show Field Keys box and copy and paste the keys into the code.

    I’m sure there’s some reason this is kind of confusing, but the fact that this simple thing isn’t in the documentation is a bit incomprehensible. Your documentation says use the field keys, great, it doesn’t say HOW TO GET THEM….

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

You must be logged in to reply to this topic.