I have an acf_form set up where users can add a new post, and the post’s title would be generated by a custom field. It’s working for adding the new post.
However, when I try to edit that post afterwards using a similar call of acf_form, I can’t get the custom field to change the post title.
I’m using this for my acf_form() call:
$args = array(
'post_id' => $post->ID,
'field_groups' => array( 4 ),
'submit_value' => 'Submit Changes',
'return' => '%post_url%'
);
acf_form( $args );
And here’s what’s in my functions.php file:
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'publish',
'post_title' => $_POST['acf']['field_1234'],
'post_type' => 'post'
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
Basically, just trying to get that custom field to change the post’s title, but I think I need a push in the right direction.
EDIT: Was able to fix this by adding to my functions file:
// check if this is to be a new post
if( $post_id != 'new' )
{
$postData = array(
'post_title' => $_POST['acf']['field_1234'],
);
wp_update_post($postData);
return $post_id;
}
ckirk, how did you get the title to set from another field?
I need that to work, but I keep getting “no-title” when the post is created.
Where did you get “field_1234”? Is it the field key?
Hi @JustinCase
You can find the field’s keys by editing a field group, clicking on ‘Screen Options’ and toggling the option to show field keys
The topic ‘How to edit post title with acf_form’ is closed to new replies.
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.