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

  • 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);