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;
}