Support

Account

Home Forums General Issues priority issue with function Reply To: priority issue with function

  • Hi @boriskamp1991

    Thanks for your patience with this problem whilst we attempt to help.

    The taxonomy field uses some different logic that others to ‘save’ the selected terms to the post. Because there may be multiple taxonomy fields saving terms (sub fields), this field type appends the selected values to an array, and then waits until later in the ‘acf/save_post’ action to save them all in 1 go.

    The good news is this is all done by the priority of 15.
    If we go back to your original code where the priority is 20, this is a good start.

    By this time (20), ACF has already saved it’s data, so I wonder if you could temporarily remove the $_POST[‘acf’] data before you re-save the post.
    This way, ACF won’t run any acf/save_post data, and will hopefully avoid any issues with re-saving the selected terms.

    Can you try this:

    
    <?php 
    	
    function my_post_title_updater( $post_id ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
    
        $posttypes = array( 'post', 'portfolio', 'recensies', 'page' );
        $currentposttype = get_post_type();
    
        if ( in_array( $currentposttype, $posttypes ) ) { //only run if is certain post-type
    		if( $currentposttype == 'post' || $currentposttype === 'portfolio' || $currentposttype === 'page' ) {
    			$my_post['post_title'] = get_field('kop');
    		} elseif( $currentposttype === 'recensies') {
    			$my_post['post_title'] = get_field('persoon') . ' (' . get_field('bedrijf') . ')';
    		}
    		
    		
    		// backup $_POST
    		$acf_post = acf_extract_var($_POST, 'acf');
    		
    		
    	    // Update the post into the database
    	    wp_update_post( $my_post );
    
    	    // restore $_POST (most likely not even needed)
    	    $_POST['acf'] = $acf_post;
    	    
    	}
    
    }
    
    // run after ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_post_title_updater', 20);
    
    ?>