Learn how to set your post title using a custom field in WordPress, and see an example of how this can be done using PHP.
As any WordPress developer knows, custom post types are a great way to extend the functionality of a website. However, there may come a time when a developer wants to use a custom field to set their custom post type title. This code example shows exactly how to do this in WordPress using PHP.
By following these simple steps, developers can easily add a custom field to their custom post type titles.
<?
add_action( 'acf/save_post', 'allen_media_set_title_cpt', 20 );
function allen_media_set_title_cpt ( $post_id ) {
$post_type = get_post_type( $post_id );
if ( 'custom_post_type' == $post_type ) {
$testimonial_field = get_field( 'custom_field', $post_id );
$title = $custom_field;
$data = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => sanitize_title( $title ),
);
wp_update_post( $data );
}
}