Support

Account

Home Forums ACF PRO Auto generate title from custom fields

Solving

Auto generate title from custom fields

  • We have a custom post type that needs no title typed in by the user. I don’t need the post_title field in front end form, but i need in backend based on the custom field.

    I’m use ACF PRO

    Here it’s my code for front end form

    <?php
    	
    	acf_form(array(
    		'post_id'		=> 'new_post',
    		'field_groups'       => array(608),
    		'post_title'	=> false,
    		'post_content'	=> false,
    		'new_post'		=> array(
    			'post_type'		=> 'companii',
    			'post_status'	=> 'draft',
    		),
    		'submit_value'       => 'Inscrie companie',
    	));
    	
    	?>
    

    And functions.php

    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    function my_pre_save_post( $post_id ) {
    
    if( $post_id == 'new_post' ) {
    
    // data send by POST in form
    $title = $_POST['fields']['field_55bf0fe0a0a81'];
    
    // Default data post
    $post = array(
    'post_status'	=> 'draft',
    'post_type'		=> 'companii',
    'post_title'	=> 'Compania |'.$title,
    );	
    // action for update
    $post_id = wp_insert_post( $post ); 
    
    }	
    }

    Can you help me ?

    Thanks in advanced

  • Use a post ID of something other than new_post. When you use that ID, ACF automatically creates the new post before your pre save post is run. You could for example use new_companii and then test for this ID is your filter.

    Also, you need to return the value of $post_id at the end of your function

    return $post_id;

  • Did you resolve this as I’m trying to do the same.

    Thanks

  • I’m having the same issue. It was working perfectly fine in v4, but something has broken in v5.

    This is my code on my page template to show the form:

    
    <?php acf_form(array(
          'field_groups' => array('group_59398ef696c50'),
      		'post_id'		=> 'new_universe',
      		'post_title'	=> false,
      		'post_content'	=> false,
      		'new_post'		=> array(
      			'post_type'		=> 'universe',
      			'post_status'	=> 'publish'
      		),
          'submit_value' => 'Create'
    		)); ?>
    

    and this is my pre_save_post function

    
    function create_universe_form( $post_id ) {
      // bail early if not a new post
      if( $post_id !== 'new_universe' ) {
        return $post_id;
      }
      // vars
      $title = $_POST['acf']['field_59398f1d71f62'];
      // Create a new post
      $post = array(
        'post_status'	=> 'publish',
        'post_type'		=> 'universe',
        'post_title'	=> $_POST['acf']['field_59398f1d71f62'],
      );
      // insert the post
      $post_id = wp_insert_post( $post );
      // Update $_POST Return
      $url = get_permalink($post_id);
    	$_POST['return'] = ($url);
      // return the new ID
      return $post_id;
    }
    
    add_filter('acf/pre_save_post' , 'create_universe_form', 1, 1 );
    

    Currently, it is properly saving/updating the URL/slug, but not the actual title.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Auto generate title from custom fields’ is closed to new replies.