Support

Account

Home Forums Front-end Issues How do you create post_title from another field(s) in ACF Pro?

Solving

How do you create post_title from another field(s) in ACF Pro?

  • Hello,

    I have two fields – name and country. On the front end I have built a form using ACF_FORM() that allows users to create a new post. when the user submits a new post from the front end, I would like to save the post title from the name and country fields, so for example, “John, Canada”. Is this possible with ACF Pro and, if so, how?

    If it is not possible. How can I simply save the post_title from one field name? E.g. Post_title: “John”.

    This is the code I am using for the form:

    acf_form(array(
       'post_id' => 'new_post',
       'new_post' => array(
          'post_type' => 'custom',
          'post_status' => 'draft'
       ),
       'field_groups'   => array(49, 4),
       'submit_value'   => 'Submit',
    ));

    I also attempted to save from one field as below, but this fails to save anything for post_title:

    acf_form(array(
       'post_id' => 'new_post',
       'new_post' => array(
          'post_type' => 'custom',
          'post_status' => 'draft',
          'post_title' => $_POST['fields']['field_53bb9a2018722'],
          '
       ),
       'field_groups'   => array(49, 4),
       'submit_value'   => 'Submit',
    ));
  • I am looking for the same thing!

  • create a new field group in functions.php then add it in your template code like this

    // in functions.php //

    
    add_action('acf/register_fields', 'register_field_group');
    
    if(function_exists("register_field_group"))
    {
        register_field_group(array (
            'id' => 'acf_form-post-title',
            'title' => 'Form Post Title',
            'fields' => array (
                array (
                    'key' => 'field_25',
                    'label' => 'Title',
                    'name' => 'form_post_title',
                    'type' => 'text',
                    'default_value' => '',
                    'formatting' => 'html',
                ),
            ),
            'location' => array (
                array (
                    array (
                        'param' => 'post_type',
                        'operator' => '==',
                        'value' => 'course',
                        'order_no' => 0,
                        'group_no' => 0,
                    ),
                ),
            ),
            'options' => array (
                'position' => 'normal',
                'layout' => 'no_box',
                'hide_on_screen' => array (
                ),
            ),
            'menu_order' => -2,
        ));
    }
    
    

    // in you template page //

    
    acf_form(array(
       'post_id' => 'new_post',
       'new_post' => array(
          'post_type' => 'custom',
          'post_status' => 'draft',
          '
       ),
       'field_groups'   => array(49, 'acf_form-post-title'),
       'submit_value'   => 'Submit',
    ));
    
  • Hi Localdev,

    I have tried this, but receive the error:

    Warning: array_merge(): Argument #2 is not an array

    in relation to the line:

    ‘field_groups’ => array(49, ‘acf_form-post-title’),

  • which ACF version you are using if its below ver.5 then use
    'post_id' => 'new',

    instead of
    'post_id' => 'new_post',

  • I am using Pro (5).

  • I am also having issues with the ‘post_title’ attribute when creating a new post. It isn’t working at all for me, when using all the documentation I see online.

    None of these work:

    'post_title'	=> $_POST['fields']['field_53dd8f9f80dad'], // NO
    'post_title'  => $_POST['acf']['field_53dd8f9f80dad'], // NO
    'post_title'  => $_POST['field_53dd8f9f80dad'], // NO
    'post_title'  => $_POST['acf-field_53dd8f9f80dad'], // NO
  • Same issue, I have a custom field that I’d like (on submit) to ALSO be used as post_title.

    I’ve tried filtering as well as using the call to the fields that robnero mentions above, no luck.

    Anyone else have success?

  • Similar issue, I want to submit a custom field from fron-end form also use it as post_content.

    no luck so far.

  • Bump. Same thing here.

    I’ve tried filtering the post title using the acf/update_value filter. But no combination of _post_title, acf-_post_title, post_title, etc. used as key= or name= seems to fire the filter.

    add_filter('acf/update_value/name=_post_title', 'cca_acf_update_post_title', 10, 3);
    function cca_acf_update_post_title( $value, $post_id, $field  )
    {
    	return "name=_post_title";
    }

    That does nothing.

  • Just messing around here. I ran this to see where the Post Title and Post Content are getting passed.

    add_filter('acf/pre_save_post' , 'cca_pre_save_post', 10, 1 );
    function cca_pre_save_post( $post_id )
    {
       $_POST['acf']['field_5419e3b4411cc'] = print_r( $_POST, true ); // textarea in form
       return $post_id;
    }

    And this was returned:

    Array
    (
        [_acfnonce] => 
        [_acfchanged] => 1
        [acf] => Array
            (
                [field_5419e384411cb] => The Business Name
                [field_5419e3b4411cc] => 
                [field_5419de9b4834f] => The Buisiness name
                [field_5419deb048350] => 
                [field_5419df0348353] => 
                [field_5419dee748352] => 
                [field_5419e441411ce] => 
                [field_5419e457411cf] => 
                [field_5419e47e411d0] => 
                [field_5419e4bb411d1] => 
                [field_5419e4c8411d2] => 
                [field_5419e4eb411d3] => 
                [field_5419e520411d4] => 
                [field_54258b963546b] => 
                [field_542490cc4e78f] => 
                [field_54258cb962f62] => 
                [field_5425c8a5fbebe] => 1
            )
    
    )
    

    Which, as you can see, doesn’t include either the Post Title or Post Content. However, looking at the Form Data in Dev Tools in Chrome, this is what’s getting passed:

    acf[_post_title]:The Post Title
    acf[_post_content]:
    acf[field_5419e384411cb]:The Business Name
    acf[field_5419e3b4411cc]:
    acf[field_5419de9b4834f]:The Buisiness name
    acf[field_5419deb048350]:
    acf[field_5419df0348353]:
    acf[field_5419dee748352]:
    acf[field_5419e441411ce]:
    acf[field_5419e457411cf]:
    acf[field_5419e47e411d0]:
    acf[field_5419e4bb411d1]:
    acf[field_5419e4c8411d2]:
    acf[field_5419e4eb411d3]:
    acf[field_5419e520411d4]:
    acf[field_54258b963546b]:
    acf[field_542490cc4e78f]:
    acf[field_54258cb962f62]:
    acf[field_5425c8a5fbebe]:1

    So … where does ‘_post_title’ go?

  • Just add:
    'post_title' => $_POST['fields']['field_yourfieldnumber']
    to your array.

    For example:

    $post = array(
        'post_status'  => 'publish' ,
        'post_title'  =>  $_POST['fields']['field_1'],
        'post_type'  => 'example' ,
    );

    I think everyone is just missing [‘fields’] before the field name.

    Compliments to Aaron Ranger for his tutorial:
    http://www.telegraphicsinc.com/2013/07/how-to-add-new-post-title-using-aacf_form/

  • @rockeypjb and @robnero

    That’s not working with PRO. I set up the front end form like this:

    acf_form( array('post_id' => 'new_post',
      'new_post' => array( 
        'post_title' => 'Temp Title',
        'post_content' => 'Temp Contents',
        'post_type' => 'members', 
        'post_status' => 'publish' ),
      'submit_value' => 'Submit',
      'field_groups' => array( 17 ),
      'return' => get_the_permalink() ) );

    I tried it without the ‘new_post’ array, knowing that’d I’d be changing the values in a pre_save_post filter, but the post wouldn’t save without it.

    Then the pre_save_post function is:

    if( $post_id == 'new_post' )
    {
      $post = array( 'post_title' => $_POST['acf']['field_2'],
        'post_content' => $_POST['acf']['field_2'],
        'post_type' => 'members', 
        'post_status' => 'publish' );
    
      $post_id = wp_insert_post( $post );
    }
    
    return $post_id;

    None of the changes made via the pre_save_post function had any effect on the post that was saved to WP.

  • @Lagom-Jim does it work like this:

    acf_form( array('post_id' => 'new_post',
      'new_post' => array( 
        'post_title' => $_POST['acf']['field_2'],
        'post_content' => 'Temp Contents',
        'post_type' => 'members', 
        'post_status' => 'publish' ),
      'submit_value' => 'Submit',
      'field_groups' => array( 17 ),
      'return' => get_the_permalink() ) );

    All on the form page, with no pre_save_post function?

  • @robnero Nope. I tried that too, but I don’t think the $_POST variables have been established before the form submits. So they can’t be passed to acf_form_head before they exist. I may be wrong on why, but it didn’t work in testing.

    As an aside, using ‘field_1’, ‘field_2’, etc. Doesn’t work at all. At least not filtering with pre_save_post or update_content_value. I’ve only had success with the nonsense-looking field names I pull off of the source code HTML that looks like ‘field_5419e384411cb’ or something similar.

  • Yes, “field_1” is only a placeholder. It should be the long “field_5419e384411cb” field names.

    Even with the long names, still no luck in any way?

  • Sorry @robnero. It’s early, I have a newborn that doesn’t sleep, and I’m only on coffee #1. Of course those are placeholders…

    No, even using the long field names, no success getting post_title or post_content to populate via $_POST variable via the acf_form function.

    I guess I could set ‘post_title’ to true in the acf_form, hide the form field via CSS/JS, have it populate from another field via JS, and then be submitted that way. Just seems like a lot of extra work to do something that should be able to be accomplished on the server side.

  • Hi guys! How do I combine multiple fields in the title? What is the syntax for concatenate?
    ‘post_title’ => $_POST[‘acf’][‘field_5481ab2cdda13’] [‘field_54814faa2f090’], //NOT work

    VER 5 PRO

  • Ugh, was this ever resolved? I’m trying everything here and still can’t get anything post_title related to work.

    EDIT: I got it working a little after, turns out while I was copying the long field number it was including and extra number.. totally wasted an hour when the number was wrong the whole time.. glad it’s working now!

  • It works? Uhhh. Post here the example of code,plz.

  • Please, the code, please 🙂
    Thanks.

  • Nope. Never figured it out. Went a different route using a different plugin.

  • This is currently working on a custom post type for me.

    /**
     * Front end creation of new download
     * @author  Mike Hemberger
     * @link http://thestizmedia.com
     * @uses Advanced Custom Fields Pro
     */
    add_filter('acf/pre_save_post' , 'jivedig_newdownload_pre_save_post' );
    function jivedig_newdownload_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_5483b32a04842'], // beat_title
    		'post_type'   => 'download',
        );
    
        // 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;
    
    }
Viewing 25 posts - 1 through 25 (of 45 total)

The topic ‘How do you create post_title from another field(s) in ACF Pro?’ is closed to new replies.