Home › Forums › Front-end Issues › Set post title on update with acf_form() › Reply To: Set post title on update with acf_form()
Thanks to ACF support I was able to solve my problem like this:
function my_auto_title( $post_id ) {
// Check if this is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check if this is a revision
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// Set the post title based on ACF fields
$title = get_field('field_65a95b23de2d4', $post_id) . ' - ' . get_field('field_65a95bd0de2d5', $post_id);
$post = array(
'ID' => $post_id,
'post_title' => $title,
);
// Remove the action to avoid infinite loop
remove_action( 'acf/save_post', 'my_auto_title', 20 );
// Update the post
wp_update_post( $post );
// Re-add the action
add_action( 'acf/save_post', 'my_auto_title', 20 );
}
add_action('acf/save_post', 'my_auto_title', 20);
Not entirely sure why the infinite loop is not triggered anymore, but leaving this here if someone has a similar problem.
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.