Support

Account

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();
                    }
                        
                  ?>