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?
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.
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);
}
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);
}
My issue is resolved now. Thank you!