Support

Account

Home Forums ACF PRO ACF_Form Generate Post Title

Helping

ACF_Form Generate Post Title

  • I’m looking for an answer how to generate a post title using acf_form. I would like to generate the title from two or three custom fields. However everything I try I cannot get the front end form to generate a post title.

    My current code for the front end form is;

    acf_form(array(
                                 'post_id' => 'new_post',
                                 'field_groups' => array(220), 
                                 'post_title' => false, 
                                 'post_content' => false,
                                 'form' => true,
                                 'new_post' => array(
                                     'post_type' => 'setups_2019',
                                     'post_status' => 'publish',
                                 ),
                                 'return' => '%post_url%',
                                 'submit_value' => 'Submit Setup',
                             ));
  • You need to create either an acf/pre_save_post or an acf/save_post filter. Since you’re using the built in acf/pre_save_post filter I would go with an acf/save_post filter.

    
    add_action('acf/save_post', 'update_setups_2019_title');
    function update_setups_2019_title($post_id) {
      if (get_post_type($post_id) != 'update_setups_2019') 
        // only run on your post type
        return;
      }
      // get the post
      $post = get_post($post_id);
      // update the post title
      $post->post_title = get_field('field-one').' '.get_field('field-two'); // etc..
      // correct the slug.. optional and I don't know that this is 100% correct
      $slug = sanitize_title($post->title);
      $post->post_name = wp_unique_post_slug($slug, $post->ID, $post->post_status, $post->post_type, $post->post_parent);
      // remove this filter to prevent infinite loop
      remove_filter('acf/save_post', 'update_setups_2019_title');
      // update the post
      wp_update_post($post);
      // re-add this filter
      add_action('acf/save_post', 'update_setups_2019_title');
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘ACF_Form Generate Post Title’ is closed to new replies.