I'm trying to find a way to update a custom post type slug when editing post: i need the slug to be identical (sanitized, obviously) to the title.
Custom post is created and edited with wpuf pro.
I'm trying to update post via wpuf_edit_post_after_update hook, but with no luck.
I'm now using this function inside my plugin:
function my_set_permalink_as_title($post_id, $post) {
if ( 'my_cpt' !== $post->post_type ) return;
$title = sanitize_title_with_dashes($post->post_title);
$my_post = array(
'ID' => $post_id,
'post_name' => $title
);
wp_insert_post( $my_post );
}
add_filter( 'wpuf_edit_post_after_update', 'my_set_permalink_as_title', 10, 2 );
Any help appreciated.
I'm just an idiot.
$post object simply doesn't exist inside my function: I have to create it.
I also have removed if statement for clarity.
function my_set_permalink_as_title($post_id, $post) {
$post = get_post($post_id);
$askb_title = $post->post_title;
$askb_slug = sanitize_title($post->post_title);
$my_post = array(
'ID' => $post_id,
'post_name' => $askb_slug,
'post_type' => 'my_cpt',
'post_title' => $askb_title,
'post_content' => '',
'post_status' => 'publish',
);
wp_insert_post( $my_post );
}
add_filter( 'wpuf_edit_post_after_update' , 'my_set_permalink_as_title' , 99, 2 );