Support

Account

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

Solving

Create product title + permalink + slug from several custom fields

  • I want to create new Woo products in backend as admin (or update), without using the default WP title field.
    The title (and permalink + slug) should always be created based on values selected in two dropdown boxes and one text field in ACF Pro (all are mandatory fields). When saving the product (also about 20 other custom fields) it should be created/updated and published. The title should be a combination of: “value 1 + 2 + 3”.

    Changes afterwards in the products default WP title field should be ignored (maybe hide that), unless any of the three custom fields that create the title have been edited. Updates in any of the three custom fields should not only update the title when saving, but also update the permalink and slug.

    I created a solution to this at least 5 years ago, but it was a slow and bad solution as I´m not a coder. I can´t find that code, but it took about 30 seconds to save a product so there were probably several errors in that code (I guess that “wp_insert_post_data” was run before the acf/save_post)! I have searched for an updated code in several forums, but I have not succeeded. I really appreciate any help with this!

    WP (5.8.1), Avada (7.5), ACF Pro (5.10.2), PHP (7.4.25). Smarter code and better performance with PHP version 8.0.12?

  • 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

  • Thanks for your reply and I´m sorry for my late answer!
    I´m found the code below that seems to work, can you find anything that´s totally wrong there?
    What´s the best way to edit the code so it can handle two different CPT (and also create the title + slug + permalink in different ways for each CPT)?

    ————————————

    function create_my_post( $value, $post_id, $field ) {
    
    $h01 = get_field( 'backendfield1' );
    $h02 = get_field( 'backendfield2' );
    $h03 = get_field( 'backendfield3' );
    $h04 = get_field( 'backendfield4' );
    $h05 = get_field( 'backendfield5' );
    $h06 = get_field( 'backendfield6' );
    
    $new_title = $h01 . ' ' . $h02 . ' ' . $h03 . ' ' . $h04 . ' ' . $h05 . ' ' . $h06;
    $new_title = trim(preg_replace('/\s+/',' ', $new_title));
    $new_slug = sanitize_title( $new_title );
    
    $postdata = array(
    'ID'          => $post_id,
    'post_title'  => $new_title,
    'post_name'   => $new_slug,
    );	
    
    if ( get_post_type() === 'specific-pt' ) {
    
    remove_action('save_post', 'create_my_post');
    wp_update_post( $postdata );
    add_action('save_post', 'create_my_post');
    }	
    
    return $value;
    }
    
    add_filter( 'acf/update_value', 'create_my_post', 10, 3);
  • So you don’t want to update the Woo product info but 2 different custom post types.

    But you want that to happen when you publish a woo product or have I misunderstood?

    My concern is how you get the post ID of both CTPs at the same time. The only way around this would be that both CPTs have an identifier.

    When you then run the update, you would need to query both post types for posts with that identifier, this would return the post IDs. You can then update those posts with whatever you need.

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

You must be logged in to reply to this topic.