I am trying to clean up the CPT Slugs for better hirarchy.
I use a Code Snippet I have found and adapted it.
It works for ONE CPT, but when I adapt it for the other CPTs I can`t activate them, what am I missing?
/**
Remove the slug from published post permalinks. Only affect our CPT though.
*/
function bis_remove_cpt_slug($post_link, $post, $leavename) {
if($post->post_type == 'gd_selbsthilfegru' && 'publish' == $post->post_status) {
$post_link = str_replace( '/selbsthilfegruppe/', '/', $post_link );
}
return $post_link;
}
add_filter('post_type_link', 'bis_remove_cpt_slug', 10, 3);
function bis_parse_request_tricksy($query) {
global $wp, $wpdb;
if(!empty($wp->query_vars['error'])) {
$slug = basename($wp->request);
$location_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts p WHERE p.post_name = %s AND p.post_status = %s AND p.post_type = %s", array($slug, 'publish', 'gd_selbsthilfegru')));
if($location_post) {
$query = array(
'name' => $slug,
'post_type' => 'gd_selbsthilfegru',
'gd_place' => $slug
);
}
}
return $query;
}
add_filter( 'request', 'bis_parse_request_tricksy', 9 );
function bis_redirect_old_place_permalinks() {
global $wp, $wpdb, $post;
if(strpos($wp->request, 'selbsthilfegruppe') === 0 && !empty($post->ID) && is_singular('gd_selbsthilfegru')) {
$canonical_permalink = get_permalink($post->ID);
wp_safe_redirect($canonical_permalink, 301);
exit();
}
}
add_action('template_redirect', 'bis_redirect_old_place_permalinks');