Home › Forums › General Issues › Using a field to define a new post title with acf_form › Reply To: Using a field to define a new post title with acf_form
Easiest way to achieve this is to update your post title using wp_update_post AFTER your acf_form() array – saves all the headache of creating a pre_save_post function and whatnot.
Example:
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
?>
<?php acf_form([
'post_id' => 'new_post',
'field_groups' => [19689],
'html_before_fields' => '<div id="formContainer">',
'html_after_fields' => '</div>',
'uploader' => 'basic',
'submit_value' => 'Submit',
'updated_message' => false,
'new_post' => array(
'post_type' => 'custompost',
'post_status' => 'publish',
)
]); ?>
<?php
//update post title using WP_Query, getting relevant post meta and passing it to the wp_update_post() function
$updatePost= new WP_Query([
'post_type' => 'custompost',
'post_status' => 'publish',
'author' => $current_user_id
]);
//loop
if($updatePost->have_posts()){
while($updatePost->have_posts()){
$updatePost->the_post();
$newTitle = get_post_meta(get_the_ID(),'field_name', true );
if($jobTitle){
wp_update_post([
'post_title' => $newTitle
]);
}
}
wp_reset_postdata();
}
?>
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.