Support

Account

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

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