Home › Forums › General Issues › Update custom post title with ACF Fields › Reply To: Update custom post title with ACF Fields
Update:
The above answer will allow you to update previously established posts but not work for new posts, so the following function addresses that and allows for new posts and previous posts to modify the title from acf fields:
function update_contacts_title($post_id) {
if( have_rows('contact_info') ):
while( have_rows('contact_info') ): the_row();
$first_name = get_sub_field('first_name', $post_id);
$last_name = get_sub_field('last_name', $post_id);
$title = $first_name . '-' . $last_name;
$slug = sanitize_title( $title );
endwhile;
endif;
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'contacts',
'post_name' => $slug
);
wp_update_post( $postdata );
}
add_filter('acf/save_post', 'update_contacts_title', 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!
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.