I'm using the following code, but I think I get a loop somewhere, my local MAMP server eventually dies with a 500 error:
function gg_fill_full_name( $post_id, $post ){
if ( 'application' == $post->post_type ) {
$firstname = get_post_meta( $post_id, 'wpcf-first-name', true );
$middlename = get_post_meta( $post_id, 'wpcf-middle-name', true );
$lastname = get_post_meta( $post_id, 'wpcf-last-name', true );
$suffix = get_post_meta( $post_id, 'wpcf-name-suffix', true );
$result = $lastname . " " . $suffix . ", " . $firstname . " " . $middlename;
if ( !empty($result) ) {
$my_post = array(
'ID' => $post_id,
'post_title' => $result,
);
// Update the post into the database
wp_update_post( $my_post, $wp_error );
}
}
}
add_action( 'save_post', 'gg_fill_full_name', 30, 2 );
![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2080%2080'%3E%3C/svg%3E)
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
Well - yes, you should not use the wp_update_post() within the save_post action.
Could you please try to use following code and try to resolve your issue:
function gg_fill_full_name( $post_id, $post ){
global $wpdb;
if ( $post_id == null || empty($_POST) )
return;
if ( wp_is_post_revision( $post_id ) )
$post_id = wp_is_post_revision( $post_id );
if ( empty( $post ) )
$post = get_post($post_id);
if ( 'application' == $post->post_type ) {
$firstname = get_post_meta( $post_id, 'wpcf-first-name', true );
$middlename = get_post_meta( $post_id, 'wpcf-middle-name', true );
$lastname = get_post_meta( $post_id, 'wpcf-last-name', true );
$suffix = get_post_meta( $post_id, 'wpcf-name-suffix', true );
$result = $lastname . " " . $suffix . ", " . $firstname . " " . $middlename;
if ( !empty($result) ) {
$where = array( 'ID' => $post_id );
$wpdb->update( $wpdb->posts, array( 'post_title' => $result ), $where );
}
}
}
add_action( 'save_post', 'gg_fill_full_name', 30, 2 );
![](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2080%2080'%3E%3C/svg%3E)
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Could you please confirm that solution I shared works for you.