Skip Navigation

[Resolved] Automatically populate a Custom Post Type Title with a Custom Field

This thread is resolved. Here is a description of the problem and solution.

Problem:
Automatically populate a Custom Post Type Title with a Custom Field

Solution:
You can use the WordPress standard hook "save_post" to override the post title.

For example:

function is_project_overdue( $post_id, $post ){
    global $wpdb;
   
     if ( 'your-post-type-slug' == $post->post_type ) {
        $custom_title = "cutom title goes here";  // adjust your custom title value here   
        $wpdb->update( $wpdb->posts, array( 'post_title' =>  $custom_title  ), array( 'ID' => $post_id ) ); 
     }
}
add_action( 'save_post', 'is_project_overdue', 30, 2 );

Where:
- Replace your-post-type-slug with your original post type slug

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Ben 6 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1114342

Ben

Is there any way I can automatically populate the Title of a Custom Post Type with a Custom Field?

I want to be able to hide the Title when editing the CPT and have it auto populated from a Custom Field within the CPT if that makes sense.

Is there any way to do this?

#1114831

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - You can use the WordPress standard hook save_post to override the post title.

For example:

function is_project_overdue( $post_id, $post ){
    global $wpdb;
  
     if ( 'your-post-type-slug' == $post->post_type ) {
        $custom_title = "cutom title goes here";  // adjust your custom title value here   
        $wpdb->update( $wpdb->posts, array( 'post_title' =>  $custom_title  ), array( 'ID' => $post_id ) ); 
     }
}
add_action( 'save_post', 'is_project_overdue', 30, 2 );

Where:
- Replace your-post-type-slug with your original post type slug

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

#1120111

Ben

Thank you Minesh.