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);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.