Home › Forums › Backend Issues (wp-admin) › Replacing custom post type post title with an acf? › Reply To: Replacing custom post type post title with an acf?
A variation of what has been posted worked for us. Unlike others we seemed to be getting trapped in an infinite loop. Here is a solution derived from some information on the WP codex. Maybe it can help you out:
function person_update_title( $value, $post_id, $field ) {
$new_title = get_field( 'person_first_name', $post_id) . ' ' . $value;
$new_slug = sanitize_title( $new_title );
// update post
$person_postdata = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $new_slug,
);
if ( ! wp_is_post_revision( $post_id ) ){
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'person_update_title');
// update the post, which calls save_post again
wp_update_post( $person_postdata );
// re-hook this function
add_action('save_post', 'person_update_title');
}
return $value;
}
add_filter('acf/update_value/name=person_last_name', 'person_update_title', 10, 3);
(If this is a duplicate, please kill it. Having troubles navigating the interwebs)
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.