Home › Forums › Front-end Issues › Set post title on update with acf_form()
My site automatically sets the post title of a CPT post, based on the value of two ACF custom fields.
I have tried the wp_insert_post_data
filter for this, which worked great from the WordPress backend for both creating and updating posts, as the he filter receives both post and ACF values through $data
and $postarr
.
However, users will be able to create and update the CPT post through a site frontend with acf_form()
. Here the wp_insert_post_data
filter doesn’t work for neither creating (no acf fields) nor updating (not triggered).
After much experimenting I was able to achieve the desired behaviour for creating my posts using the save_post_{cpt}
filter like this:
function my_auto_title( $post_id, $post ) { // -> use this for create!
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( 'trash' === $post->post_status ) {
return;
}
$title = $post->post_title;
if (isset($_POST['acf'])) {
$title = $_POST['acf']['field_65a95b23de2d4'] . ' - ' . $_POST['acf']['field_65a95bd0de2d5'];
$post->post_title = $title;
}
$post_update = array(
'ID' => $post->ID,
'post_title' => $title
);
remove_action( 'save_post_my_cpt', 'my_auto_title', 20 );
wp_update_post( $post_update );
add_action( 'save_post_my_cpt', 'my_auto_title', 20 );
}
add_action( 'save_post_my_cpt', 'my_auto_title', 20, 2 );
But sadly save_post_{cpt}
is not triggered by updating my CPT post through acf_form()
.
I have tried to use acf/save_post
as a replacement, but haven’t gotten it to work. I try to use it like this:
remove_action( 'acf/save_post', 'my_auto_title', 5 );
$post_id = wp_update_post($post);
add_action( 'acf/save_post', 'my_auto_title', 5 );
return $post_id;
}
add_action('acf/save_post', 'my_auto_title', 5);
But it does not seem like the action is unhooked successfully, as this triggers an infinite loop that weirdly ends up overwriting the page I am on instead of the post.
I have also tried wp_insert_post
instead of wp_update_post
, but that leaves me with two separate entries for post and ACF fields.
I would be looking for a way to set the post title based on my two ACF fields for post updates as well.
Also, if there is a way to use the same hook for create and update I could simplify my code.
Thank you!
ACF Pro 6.2.5
Yes, you need to use acf/save_post instead of WP hooks.
When using this hook you need to use WP functions to get the other information since it only passes the post ID and not the post data.
There are many topics on this forum about updating the post title from an ACF value.
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.
You must be logged in to reply to this topic.
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.