Home Forums General Issues Using a field to define a new post title with acf_form


Using a field to define a new post title with acf_form

  • Hey guys,

    I’m creating a tool for a client which I want to allow front end post creation. I’m utilising acf_form as the title suggests, however the issues I’m running into are two fold.

    Firstly when acf_form posts the new data, it’s creating two post entries in the back end.

    Secondly the post entries have no post title.

    This is the code I’m working with;

    <?php $options = array(
    	'post_id'		=> 'new_post',
    	'post_title' => $_POST['fields']['field_5786476b9c451'],
    	'fields' => array(
    	'new_post'		=> array(
    		'post_type'		=> 'projects',
    		'post_status'		=> 'publish'
    	'submit_value'		=> 'Add Project'
    acf_form($options); ?>

    As I mentioned, the individual posts are added but they’re duplicated and without a title.

    Any help would be appreciated.


  • Hi @edcraddock

    The ‘post_title’ value should be false or true, not the title you want. This option will add a field that allows you to add the title on the front end.

    Regarding the duplicated post, I’m not sure why. Maybe there’s a function on your site that caused this issue. Could you please try to reproduce the issue on one of the WordPress’ stock themes (like Twenty Sixteen) with other plugins deactivated? If it disappears, then you can activate the theme and plugins one by one to see which one causes the issue.

    Thanks 🙂

  • Hey James,

    Okay, gotcha. So if I wanted to take a field from the form and use the value as the post title, this isn’t possible, or am I just going about it the wrong way?

    I will do a little troubleshooting for the duplicated post. If I have other actions which hook into ‘acf/save_post’, could that be potentially contributing to the issue do you think?


  • Hi @edcraddock,

    I’m trying to do the same thing. Basically, what you need to do is add a hook on saving the post, and assemble the title from the components.

    add_action('acf/save_post', 'my_save_post');
    function my_save_post($post_id){
      // make sure this post type is the kind we want
      // get the first and last name fields
      // (or whatever field(s) you want to convert into the title
      // update the post title (if there isn't one already)

    I’m using this help page to figure this out: (look at the contact form example)

    the acf/save-post hook happens AFTER the post is inserted, so you can use wp_update_post to update the title.

    (I’ll post my code here once I actually get it working)

  • Hi @edcraddock

    is correct. You should use the acf/save_post and wp_update_post() to do that. Also, don’t forget to remove the hook before calling the wp_update_post() function to avoid the infinite loop issue. It should be something like this:

    add_action('acf/save_post', 'my_save_post', 20);
    function my_save_post($post_id){
      // Get the data from a field
      $new_title = get_field('title_field_name', $post_id);
      // Set the post data
      $new_post = array(
          'ID'           => $post_id,
          'post_title'   => $new_title,
      // Remove the hook to avoid infinite loop. Please make sure that it has
      // the same priority (20)
      remove_action('acf/save_post', 'my_save_post', 20);
      // Update the post
      wp_update_post( $new_post );
      // Add the hook back
      add_action('acf/save_post', 'my_save_post', 20);

    I hope this helps 🙂

  • Hey guys,

    Thank you both for the help. In the end I followed the advice of James, which was absolutely spot on.

    I’m still having an issue with the duplicate posting however, and I am running on twentysixteen. I’ve not done a great deal of troubleshooting on that as of yet, but thought I’d mark the first issue as solved whilst I do a bit more research.

    Once again, thanks for the help!

  • 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.


    $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',
    				]); ?>
           //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
                     $newTitle = get_post_meta(get_the_ID(),'field_name', true );
                          'post_title'        =>      $newTitle
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Using a field to define a new post title with acf_form’ is closed to new replies.