Support

Account

Home Forums Front-end Issues Frontend form for new post not saving taxonomy terms

Solved

Frontend form for new post not saving taxonomy terms

  • I can’t get a frontend form (for a new post) to save taxonomy values. A taxonomy field is setup and mapped to the state taxonomy. The field displays properly on the frontend form. It just won’t save the selection.

    
    $default_fields = array('field_558884854c6ce','field_558884974c6cf','field_5591b6e43e1b7');
    // fields: first_name, last_name, state
    
    $args = array(
    	'post_id'		=> 'new_post',
    	'new_post'		=> array(
    		'post_type'	=> 'tribute',
    		'post_status'	=> 'draft',
    	),
    	'fields'		=> $default_fields,
    	'submit_value'		=> __('Continue'),
    	'updated_message' 	=> __("The tribute has been saved", 'acf'),
    );
    acf_form($args);
    

    The frontend form will save the custom taxonomy just fine when editing an existing post. It’s the creation of a new post that’s giving me trouble.

    acf_form_head() is included in the head. It saves all other data. Just won’t save taxonomies (either custom or default).

    Thanks!

  • Wondering if it’s a problem with the taxonomy not being registered at the time the post is saved? Seems that everything should be registered at that stage – looking into that today.

    The taxonomy field is set to load/save with the actual taxonomy.

  • Narrowing it down here…

    The problem is in the use of acf/save_post.

    Quick overview: I have a multi-step process setup for creating a tribute (custom post type). First form creates the post (a custom post type). Next forms are for adding additional meta data. To get users to step 2, I am using the acf/save_post action to redirect them (back to the editor) after the post is created. However, using this action is preventing the custom taxonomy (states) from saving. It saves the text fields just fine.

    function get_tribute_editor_url($trib_step=false,$trib_id=false) {
    	
    	$url = site_url() . '/edit-tribute/';
    	
    	if ($trib_step && $trib_id) {
    		$url .= "?trib_step=$trib_step&trib_id=$trib_id";	
    	}
    	
    	return $url;
    }
    
    function acf_post_save_tribute( $post_id ) {
    	
    	// bail early if not a tribute
    	if( get_post_type($post_id) !== 'tribute' ) {
    		return;
    	}
    	
    	// get step
    	$trib_step = get_field('trib_step', $post_id);
    	
    	// create/update the post title if this is a new post or if the user has edited step 1
    	if ( $trib_step <= 1 ) {
    		$args = array(
    			'ID' 		 => $post_id,
    			'post_title' => $_POST['acf']['field_558884854c6ce'] . ' ' . $_POST['acf']['field_558884974c6cf'],
    		);
    		wp_update_post($args);
    	}
    
    	// redirect	new tributes to step 2 and pass along the proper ID
    	if ($trib_step < 1) {
    		$redirect = get_tribute_editor_url(2,$post_id);
    		wp_redirect($redirect);
    		exit;		
    	}
    }
    add_action( 'acf/save_post', 'acf_post_save_tribute', 20);
    
  • I’m not sure if this is THE solution. I’d absolutely love to NOT have to manually update the Taxonomy, but have ACF handle it – like it seems it should do? Totally may be user error for why it’s not.

    I’m manually saving the taxonomy using wp_set_object_terms(). In this case the user is restricted to selecting a single term. The solution would look a bit different if multiple terms were allowed.

    
    function acf_post_save_tribute( $post_id ) {
    	
    	// bail early if not a tribute
    	if( get_post_type($post_id) !== 'tribute' ) {
    		return;
    	}
    
    	// bail early if wordpress admin
    	if( is_admin() ) {
    		return;
    	}
    	
    	// get step
    	$trib_step = get_field('trib_step', $post_id);
    	
    	// create/update the post title if this is a new post or if the user has edited step 1
    	if ( $trib_step <= 1 ) {
    		$args = array(
    			'ID' 		 => $post_id,
    			'post_title' => $_POST['acf']['field_558884854c6ce'] . ' ' . $_POST['acf']['field_558884974c6cf'],
    		);
    		wp_update_post($args);	
    		
    		// manually update the state taxonomy field
    		$the_term_obj = get_term($_POST['acf']['field_5591b6e43e1b7'], 'trib_state');
    		$the_term_slug = $the_term_obj->slug;
    		wp_set_object_terms($post_id, $the_term_slug, 'trib_state');
    	}
    
    	// redirect new tributes to step 2 and pass along the proper ID
    	if ($trib_step < 1) {
    		$redirect = get_tribute_editor_url(2,$post_id);
    		wp_redirect($redirect);
    		exit;		
    	}
    
    }
    
  • I know this thread is old, but I’m still running into this problem when saving taxonomies on the front end as well. As noted it seems to happen when using the acf/save_post function to redirect users during a multi-steps post creation process. That seems to be the common denominator at least.

    It can indeed be worked around via the part of the function above, but it would be great if anyone on here had any insight into why the taxonomies aren’t saved as they should be based on the ACF settings and if there is a better method of using acf/save_post that will allow ACF to do its magic alongside it.

  • Hi @arcanepsyche

    Could you please share the JSON or XML export of the field group, the code to create the multi-steps form and the hook code that isn’t working?

    Looking at @apmeyer’s code, I believe it will produce an infinite loop in the saving process. To avoid it, please take a look at this page: https://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops.

    Thanks!

  • Sure! I’m not using the exact code above, just a similar method. Here is the php code i’m using to display the form as well as the custom function.

    The below code works to manually save the taxonomies, but if I remove that portion of the function the taxonomies don’t save.

    NOTE: I have the taxonomy field set to NOT save to the post at the moment just in case it interfered with me doing manually. But when troubleshooting, I had it turned on.
    Also note, there are fields called “columns” which are reliant on another plugin. They just change the layout of the fields and are not important to the actual content of the form.

    On page template:

    <div id="new-audition-form" class="hide-submit">
    				<?php 
    				$new_audition = array(
    					'post_id' => 'new_post',
    					'new_post' => array(
    						'post_type' => 'audition',
    						'post_status' => 'draft',
    					),
    					'post_title' => true,
    					'post_content' => true,
    					'submit_value' => 'Continue >',
    					'return' => '%post_url%',
    				);
    				acf_form($new_audition);
    				?>
    				</div>

    Function:

    //ACF Front-End Audition Functions
    //Redirect to the terms and conditions page on the first post save
    function tps_acf_save_audition( $post_id ) {
        if (!is_admin()) {
    		
    		// manually update the audition_type taxonomy field
    		$term_object = get_term($_POST['acf']['field_5701d3e794a6f'], 'audition_type');
    		$term_slug = $term_object->slug;
    		wp_set_object_terms($post_id, $term_slug, 'audition_type');
    			
    			
    		if (get_post_status($post_id) == 'draft') {
    			wp_redirect(get_bloginfo('url').'/auditions/terms-and-conditions?auditionid='.$post_id);
    			exit;
    		}
    	}
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'tps_acf_save_audition', 20);

    After the user is redirected to the terms and conditions page, they press a button that publishes the post, completing the process.

  • Hi @arcanepsyche

    I believe it’s because you didn’t set the “Save Terms” option to “Yes”, but set the “Load Terms” to “Yes”. Could you please set both of the to “Yes” and see if it fixes the issue? I’ve attached a screenshot for your reference.

    Thanks!

  • Hi James, sorry for the delay. I currently have those settings turned off, but when I was encountering the bug I had them turned on, as I mentioned above. So even with the save and load settings turned ON, they are not saving to the post for some reason.

  • Hi @arcanepsyche

    It seems that the wp_redirect() and exit function prevents the term saving. If you remove that code, you’ll see that the term is saved correctly. If you need to use the redirect code, I’m afraid you need to use the wp_set_object_terms() function in that hook. I’m sorry for the inconvenience.

    I hope this helps.

  • Would like to add that 4+ years later, this is still an issue. Same setup for me:

    • Frontend form
    • Taxonomy field saving terms to post
    • Redirecting in acf/save_post

    I mostly understand why this is happening. But to avoid needing to use wp_set_object_terms(), is it possible for me to use another acf filter that fires after the terms are normally saved to the post in order to finish off the redirect?

  • Okay, I asked the question and then immediately answered it myself. You can see in includes/fields/class-acf-field-taxonomy.php line 15 that the terms are saved to the post with a priority of 15. If you don’t specify the priority of your acf/save_post function, it defaults to 10, which will run before the terms save. The easy fix for me was the change my function priority to 20. Everything still works and now my terms are saving on new posts!

  • I thought i was going crazy.

    The taxonomy terms for existing posts will return normally while the new post won’t return anything in the save_post hook.

    I have three settings checked : Create, Save, Load terms.

    Setting priority to 20 (from 10) seems to have solved the problem. Thanks @gingerlyco.

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

The topic ‘Frontend form for new post not saving taxonomy terms’ is closed to new replies.