5

I am using the Advanced custom fields plugin and Custom post type UI to give my users some extra functionality. The problem I have is that I have setup a users information menu and in the list view all new posts appear as Auto Draft. Is there anyway I can make the field slug company name act as the post title for the list view?.

I tried the below code but it is not updating the company name as post title and in the custom post page it display message like "You are currently editing the page that shows your latest posts."

My Sample code:

add_filter('title_save_pre', 'save_title');
function save_title() {
        if ($_POST['post_type'] == 'users') : // my custom post type name
          $new_title = $_POST['company_name']; // my custom field name
          $my_post_title = $new_title;
        endif;
        return $my_post_title;
}
Dinesh
  • 197
  • 1
  • 12
  • http://wordpress.stackexchange.com/questions/54711/title-save-pre-on-post-publish – milan kyada Aug 09 '16 at 17:48
  • Have you looked at this? [http://wordpress.stackexchange.com/questions/19854/custom-post-type-with-custom-title](http://wordpress.stackexchange.com/questions/19854/custom-post-type-with-custom-title) That should work. – HostAgent.NET Sep 01 '16 at 03:13

3 Answers3

1

this should work:

add_action( 'acf/save_post', 'save_post_handler' , 20 );
function save_post_handler( $post_id ) {
    if ( get_post_type( $post_id ) == 'users' ) {
        $title              = get_field( 'company_name', $post_id ); 
        $data['post_title'] = $title;
        $data['post_name']  = sanitize_title( $title );
        wp_update_post( $data );
    }
}
chrispo
  • 29
  • 3
1

Use name="post_title" in your input.

<input type="text" name="post_title" id="meta-text" class="form-control" value="">
Billu
  • 2,733
  • 26
  • 47
0

disclaimer: I literally just read the php website top to bottom yesterday, but I read a few posts about trying to do this and assembled this solution that is working for me. I have a custom post type called artists, I combine the artist acf field of first_name and last_name and set that as the title. For your example you could remove the parts that add last name.

// Auto-populate artist post type title with ACF first name last name.
function nd_update_postdata( $value, $post_id, $field ) {
// If this isn't an 'artists' post type, don't update it.
if ( get_post_type( $post_id ) == 'artists' ) {   
    $first_name = get_field('first_name', $post_id);
    $last_name = get_field('last_name', $post_id);
    $title = $first_name . ' ' . $last_name;
    $slug = sanitize_title( $title );
    $postdata = array(
         'ID'      => $post_id,
         'post_title'  => $title,
         'post_type'   => 'artists',
         'post_name'   => $slug
    );
wp_update_post( $postdata, true );
return $value;
}
}
add_filter('acf/update_value/name=first_name', 'nd_update_postdata', 
10, 3);
add_filter('acf/update_value/name=last_name', 'nd_update_postdata', 10, 
3);