Support

Account

Home Forums General Issues Set custom post type title by acf?

Solved

Set custom post type title by acf?

  • So far I got this, but I’m unsure on how to retrieve an Advance custom fields from the post and replace ‘Toy-‘ with it.

    function add_custom_title( $data, $postarr ) 
    {
    
        if($data['post_type'] == 'toy') 
        {
            if(empty($data['post_title'])) 
            {
                $count = get_option('toy_count', 1);
                $data['post_title'] = 'Toy-' . $count++;
                update_option( 'toy_count', (int) $count );
            }
        }
    
        return $data;
    
    add_filter('wp_insert_post_data', 'add_custom_title', 10, 2 );
    
  • Hi @elbowpham

    I would fist look at the varible $data and find out what you can play with. This is more commonly known as variable ‘debugging’. You can do this like so:

    
    <?php 
    
    echo '<pre>';
    	print_r($data);
    echo '</pre>';
    die;
    
     ?>
    

    Using this code will display the contents of $data.
    You should see that the custom field value is something like:

    
    $data['fields']['field_1235678']
    

    The field_1235678 is the field_key which you can find from the field edit screen, or simply just figure it out from the posted $data

    Cheers
    E

  • Below is what I get out from the following command. However I don’t see any any fields types.

    I have a custom post type set up and I’m using acf with it if that helps.

    print_r($data);

    Array
    (
        [post_author] => 1
        [post_date] => 2013-08-01 15:02:51
        [post_date_gmt] => 2013-08-01 15:02:51
        [post_content] => 
        [post_content_filtered] => 
        [post_title] => 
        [post_excerpt] => 
        [post_status] => publish
        [post_type] => toy
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => toy-124
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2013-08-01 15:02:57
        [post_modified_gmt] => 2013-08-01 15:02:57
        [post_parent] => 0
        [menu_order] => 0
        [guid] => http://toy.dev/?post_type=toy&p=211
    )
  • Hi @elbowpham

    The $data must only contains the post data, not the $_POST data.

    Perhaps you need to look in the $_POST array

  • Thanks a lot elliot.

  • Am I correct in saying the following:

    If I named my field field_season for example, but it shows up in $_POST as field_5470f6551f722, this key is the internal reference that ACF will always use for field_season and it’s safe writing logic based on field_5470f6551f722 always referring to field_season?

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

The topic ‘Set custom post type title by acf?’ is closed to new replies.