Skip Navigation

[Resolved] Trying to populate the WP Title field with a few other fields..

This support ticket is created 6 years, 5 months 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 Minesh 6 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1099279

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 );

#1099339

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 );
#1100233

Minesh
Supporter

Languages: English (English )

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

Could you please confirm that solution I shared works for you.