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?

  • Hi,guys!
    Solution for ACF PRO 5

    To concatinate strings, you do so like this:
    $string = 'foo' . 'bar';
    
    So ACF $_POST data would look like:
    'post_title' => $_POST['acf']['field_5481ab2cdda13'] . ' ' . $_POST['acf']['field_5481ab2cdda14'],
  • Sorry not good here …

  • No it does not change anything for me.

    Anyway, I do not understand one thing, acf/pre_save_post is fired when the post is saved in the data base no ? when I try to create new record, acf/pre_save_post is not used befor acf_form() is used no ?

    If I look in the sources of my page when acf_form() is called, I see a field names someting like field_5483b32a04842 is used. I am wondering : how could I pre load a value in that field, then the user could validate it without retyping it …

    No help ?

  • ***************
    The following function should be able to help you get the value and assign it to the post
    function get_acf_title( $post_id ){
    $title = get_field( ‘title’, $post_id );

    if( empty( $title ) ){
    $title = get_the_title( $post_id );
    }

    if( $title == ‘-‘ ){
    $title = ”;
    }

    return apply_filters( ‘the_title’, $title );
    }
    **********************************

  • Having the same trouble with ACF PRO 5. Any ideas there?

  • I have a working code example earlier in this thread.

  • OK. JiveDig’s solution works just fine… but is there a way to do it without using acf/pre_save_post? ACF 5 Pro style? I want to know this because on my website I have two different forms and using acf/pre_save_post is causing conflicts.

  • MattNPL, how about conditionally using pre_save_post? … so it only fires the right code based on the group ID get_post_type, or similar…

  • Thank you for your answer – it gave me some ideas :). Yes, rather “or similar”… As I understand it by using pre_save_post you effectively declare post type before saving anything(in hooked function), so I can’t get_post_type before I have any kind of post and that’s because I have both of my end-froms on pages with custom templates, but I can use is_page to know from which of these pages I get my data. But there is another but – I have WPML installed and more than four languages on my website so solution above will bring me to suicidal thoughts. I was hoping to do this using new_post in acf_form(), but there is this problem with post_title. Back to square one.

    Sorry for my bad english.

  • I see the confusion.. I was thinking to hook in earlier, and only run the filter based on the conditional…

    add_action( 'wp_head', 'prefix_do_the_acf_trickery' );
    function prefix_do_the_acf_trickery() {
        if ( is_page('page-one') ) {
        	add_filter('acf/pre_save_post' , 'prefix_page_ONE_acf_pre_save_post', 10, 1 );
        }
        if ( is_page('page-two') ) {
        	add_filter('acf/pre_save_post' , 'prefix_page_TWO_acf_pre_save_post', 10, 1 );
        }
    }
    
    prefix_page_ONE_acf_pre_save_post() {
    	// Do your thing
    }
    
    prefix_page_TWO_acf_pre_save_post() {
    	// Do your thing
    }

    then instead of is_page() you could use get_post_type() or whatever makes sense.

    Pardon the rough code, just slapped together an example.

  • JiveDig,

    Thanks for replying to my thread. I have tried your solution, but it is not working at all for me.

    I have placed your code above acf_form_head() on my submission page template, replaced ‘download’ with my own custom post type and even tried replacing ‘$_POST[‘acf’][‘field_5483b32a04842′]’ with ‘test’.

    Nothing happens. The post saves but the title still remains empty.

    Are you sure this applies to ACF 5? If so, do you have any ideas what I might be doing wrong?

  • Hey Trumpeter.. it seems like you’re not fully understanding what’s happening with these filters. ‘download’ in my example is the post type you are posting to, not the custom field.

    I’m definitely using ACF Pro for this too.

    A lot of people seem to be struggling with this, so I wrote a tutorial explaining it in more detail.. check it out:

    http://thestizmedia.com/front-end-posting-with-acf-pro/

  • That was a typo, I meant ‘custom post type’.

    But thanks for posting the tutorial. I figured out what was wrong. In acf_form I had ‘new_post’ for ‘post_id’ instead of ‘new’.

  • This works for me

    add_action('acf/save_post', 'custom_acf_save_post', 20);
    function custom_acf_save_post( $post_id ) {
    	
    	// bail early if no ACF data
    	if( empty($_POST['acf']) ) {
    		
    		return;
    	}
    
    	$data['ID'] = $post_id;
    	// Combine first name and last name to create a title
    	$title = trim($_POST['acf']['field_55687a737d49f']) . " " . trim($_POST['acf']['field_55687a917d4a0']);
    	$data['post_title'] = $title;
    	$data['post_name'] = sanitize_title( $title );
    
    	wp_update_post( $data );
    
    }
  • @locomo your solutions works with ACF 5.3.0. The rest didn’t.

    *update*

    However this code conflicts with editing post titles from the back-end.

  • The following works for me:

    <?php
    /*
    Plugin Name: Voucher Posts
    Description: Add voucher posts
    Author: jibbius
    Text Domain: voucherpost
    Version: 1.0
    */
    
    class VoucherPost {
    	function __construct() {
    		$this->post_type   = 'voucher';
    		$this->text_domain = 'voucherpost';
    
    		add_action( 'init', array( &$this, 'register_this_post_type' ) );
    		add_action( 'acf/save_post', array( &$this, 'save_post_handler' ), 20 );
    	}
    
    	//Registers the post type
    	function register_this_post_type() {
    		register_post_type( $this->post_type,
    			array(
    				'labels'          => array(
    					'name'               => __( 'Vouchers', $this->text_domain ),
    					'singular_name'      => __( 'voucher', $this->text_domain ),
    					'all_items'          => __( 'All vouchers', $this->text_domain ),
    					'add_new'            => __( 'Add New', $this->text_domain ),
    					'add_new_item'       => __( 'Add New voucher', $this->text_domain ),
    					'edit'               => __( 'Edit', $this->text_domain ),
    					'edit_item'          => __( 'Edit voucher', $this->text_domain ),
    					'new_item'           => __( 'New voucher', $this->text_domain ),
    					'view_item'          => __( 'View voucher', $this->text_domain ),
    					'search_items'       => __( 'Search vouchers', $this->text_domain ),
    					'not_found'          => __( 'Nothing found in the Database.', $this->text_domain ),
    					'not_found_in_trash' => __( 'Nothing found in Trash', $this->text_domain ),
    					'parent_item_colon'  => ''
    				),
    				'description'     => __( 'A voucher', $this->text_domain ),
    				'public'          => false,
    				'show_ui'         => true,
    				'query_var'       => true,
    				'menu_position'   => 5,
    				'menu_icon'       => 'dashicons-tickets-alt',
    				'capability_type' => 'post',
    				'hierarchical'    => false,
    				'supports'        => false
    //              Alternatively:
    //              supports' => array('title','thumbnail'),
    			)
    		); /* end of register post type */
    	}
    
    	/* 'On save' events */
    	function save_post_handler( $post_id ) {
    		if ( get_post_type( $post_id ) == $this->post_type ) {
    			$data['ID']         = $post_id;
    			$title              = get_field( 'voucher_id', $post_id );
    			$data['post_title'] = $title;
    			$data['post_name']  = sanitize_title( $title );
    			wp_update_post( $data );
    		}
    	}
    }
    
    new VoucherPost();

    In response to;
    “However this code conflicts with editing post titles from the back-end.”
    (above)…

    My code works, whether the post is from the backend OR frontend.
    If you wanted to disable behaviour in backend, you could add this somewhere:
    if(is_admin()) return;

  • This worked for me. Thank you

  • It turned out to be quite simple. Just combine the PRO version and the older acf/pre_save_post like this:

    1.
    In the template, leave out the ‘new_post’ field:

    $options = array(
    	'id' => 'acf-form',
    	'post_id' => new_post, //if new post
    	/*Leave this field out of the array!!
    	'new_post' => array(
    			'post_title' => 'Test'
    			),
    	
    	*/
    	//...you other options
    );

    2.
    In the acf/pre_save_field in functions/template, create the post here instead:

    function my_pre_save_post( $post_id ) {
     
          // Create a new post
          $post = array(
          	'post_status'  => 'publish' ,
       	//Use the ACF fields to create the title or any other field.
          	'post_title'  => $_POST['acf']['field_586e6ada97b7b'] . ' ' . $_POST['acf']['field_586e6ae197b7c']
          );  
    
          // insert the post
          $post_id = wp_insert_post( $post );
       	 // return the new ID
       	 return $post_id;
        }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 1, 1 );

    As described earlier, only the ACF fields are included in the $_POST.

Viewing 20 posts - 26 through 45 (of 45 total)

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