Skip Navigation

[Resolved] The new item of the custom post type is created properly but not the slug

This support ticket is created 4 years, 11 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 4 replies, has 2 voices.

Last updated by Joan 4 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1418031

I create items of a CPT in a form through the code

$my_post = array(
'ID' => $post_id,
'post_title' =>$nom);

if(isset($post_id,$nom)){
wp_update_post( $my_post);
}

if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
}

if(isset($num)){
update_post_meta($post_id, 'wpcf-num', $num);
}

if(isset($metod)){
update_post_meta($post_id, 'wpcf-metod', $metod);
}

The CPT item is created properly but not the slug. The title of the CPT item is correct but the slug is saved as cred-auto-draft-9253242342342348117048778776407-3/.

What can I do in order to get the slug like the title?

#1418105

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I would like to know what form hook you are using to wrap the code you shared. Can you please share the full code with the hook you are using that is responsible to trigger the code you shared.

#1418107

The hook is cred_save_data. Sorry.

add_action('cred_save_data', 'guarda',10,2);
function guarda($post_id, $form_data){
//Guardar primer soci
if ($form_data['id']==333){

$_us = wp_get_current_user();
$us_id= $us->ID;

//Obtenir les dades de l'usuari

$nom=get_user_meta($us_id,'wpcf-nom',true);
$num=get_user_meta($us_id,'wpcf-num',true);
$metod=get_user_meta($us_id,'wpcf-reg',true);
$post_id_ini=$post_id;

if($metod==92){
//Save user datai
$my_post = array(
'ID' => $post_id,
'post_title' =>$nom);

if(isset($post_id,$nom)){
wp_update_post( $my_post);
}

if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
}

if(isset($num)){
update_post_meta($post_id, 'wpcf-num', $num);
}

if(isset($metod)){
update_post_meta($post_id, 'wpcf-metod', $metod);
}

#1418183

Minesh
Supporter

Languages: English (English )

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

It looks like you just need to add the post_name to update the post slug.

Please replace the following code with your original code that is added just after if($metod==92){ and until the wp_update_post() funciton.

//Save user datai
$slug = sanitize_title($nom);

$my_post = array(
'ID' => $post_id,
'post_title' =>$nom,
'post_name' => $slug
);

if(isset($post_id,$nom)){
wp_update_post( $my_post);
}
#1421407

My issue is resolved now. Thank you!