Support

Account

Home Forums ACF PRO Create product title + permalink + slug from several custom fields Reply To: Create product title + permalink + slug from several custom fields

  • Hi @stockholm

    I think you need something like the below:

    <?php
    add_action( 'transition_post_status', 'acf_generate_custom_url', 10, 3 );
    function acf_generate_custom_url( $new_status, $old_status, $post ) {
    
        #only apply this to a particular post type 	
    	if ( 'publish' !== $new_status or 'publish' === $old_status || 'product' !== get_post_type( $post ) )
    	return;
    	
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    	return;	
            
            #get custom fields
            $field_1 = get_field('field_1');
    		$field_2 = get_field('field_2');
    		$field_3 = get_field('field_3');
    	
    		#join field data
    		$new_permalink = $field_1.' '.$field_2.' '.$field_3; #you can remove the space if you need      
                    
            #get the post data
            $my_post = array(
                'ID'        	=> $post->ID,
    			'post_title'	=> $new_permalink, 	#update title
                'post_name'		=> $new_permalink	#update slug
            ); 
        
            #update the post into the database
            wp_update_post( $my_post );
    
    }

    Code is untested but should get you pretty close