Skip Navigation

[Resolved] Build post title

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

Problem:
Want to build the post title for Products CPT only.
Solution:
You can use get_post_type function.

add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data) {
    if ( get_post_type( $post_id ) == 'slug_post_type' ) {
        if ($form_data['id']==98 || 216) {
            $field1 = get_post_meta($post_id, 'wpcf-name-1', true);
            $field2 = get_post_meta($post_id, 'wpcf-name-2', true);
              
                
            $post_title=$field1.' + '.$field2;
            $slug = sanitize_title($post_title);
            wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
        }
    }
}

Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_post_type/

This support ticket is created 6 years, 9 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Nicholas 6 years, 9 months ago.

Assisted by: Noman.

Author
Posts
#616040

Hello I have this code

add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data) {
 
if ($form_data['id']==98 || 216) {
$field1 = get_post_meta($post_id, 'wpcf-name-1', true);
$field2 = get_post_meta($post_id, 'wpcf-name-2', true);

  
$post_title=$field1.' + '.$field2;
$slug = sanitize_title($post_title);
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
}
}

However this code builds the post title like that for all post types.
I'd like to limit it to products only.

Please help.

Regards,
Nick

#616085

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Nicholas,

Thank you for contacting Toolset support. You can use get_post_type function.

Here is updated code:

add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data) {
	if ( get_post_type( $post_id ) == 'slug_post_type' ) {
		if ($form_data['id']==98 || 216) {
			$field1 = get_post_meta($post_id, 'wpcf-name-1', true);
			$field2 = get_post_meta($post_id, 'wpcf-name-2', true);
			 
			   
			$post_title=$field1.' + '.$field2;
			$slug = sanitize_title($post_title);
			wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
		}
	}
}

⇒ Please update ‘slug_post_type’ with ‘product’ cpt slug.

https://developer.wordpress.org/reference/functions/get_post_type/

Thank you

#616120

Thanks, Noman.