I use the code below to force WordPress to generate post slug from post title even when a user define a different slug or duplicate another slug but i want this to only affect a specific post type called job_listing but unfortunately it affects all post type on my site.
function myplugin_update_slug( $data, $postarr ) {
if ( ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
return $data;
}
add_filter( 'wp_insert_post_data', 'myplugin_update_slug', 99, 2 );
How do i target only a specific post type called job_listing with the above code without affecting other post types
Can somebody help me amend this code to suit my need?
Your help will be appreciated.
CodePudding user response:
According to the docs $data
contains post_type
so you should be able to use this:
function myplugin_update_slug( $data, $postarr ) {
if ( $data['post_type'] === 'job_listing' && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
return $data;
}
add_filter( 'wp_insert_post_data', 'myplugin_update_slug', 99, 2 );