I have multiple custom post types with custom taxonomies. I'm having a slug clash despite of having different parents.
Here is my URL structure: /work/%client_name%/%project_name%
I have a client (client1) and project (some-cool-project-name) that generates this slug: "/work/client1/some-cool-project-name".
When I create a new post under a different parent (client) and give the same name (and slug) to the post, wordpress appends -2 to the slug: "/work/client2/some-cool-project-name-2"
Custom post type as:
// Custom taxonomies.
function custom_taxonomies() {
$args = array(
'label' => __( 'Work', '' ),
'labels' => array(
'name' => __( 'Work', '' ),
'singular_name' => __( 'Work', '' ),
),
'description' => '',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_rest' => false,
'rest_base' => '',
'has_archive' => true,
'show_in_menu' => true,
'exclude_from_search' => false,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'work/%client_name%', 'with_front' => true ),
'query_var' => true,
'menu_icon' => 'dashicons-hammer',
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'page-attributes' ),
'taxonomies' => array( 'client_name' ),
);
register_post_type( 'work', $args );
$args = array(
'label' => __( 'Clients', '' ),
'labels' => array(
'name' => __( 'Clients', '' ),
'singular_name' => __( 'Client', '' ),
),
'public' => true,
'hierarchical' => false,
'label' => 'Clients',
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'work/client_name', 'with_front' => false, ),
'show_admin_column' => false,
'show_in_rest' => false,
'rest_base' => '',
'show_in_quick_edit' => false,
);
register_taxonomy( 'client_name', array( 'work' ), $args );
}
add_action( 'init', 'custom_taxonomies' );
And my permalink rewrite:
// Replace URL with proper taxonomy structure.
function permalink_rewrites( $link, $post ) {
if ( $post->post_status !== 'publish' || $post->post_type != 'work' ) {
return $link;
}
if ( $post->post_type == 'work' ) {
$type = '%client_name%/';
$filters = get_the_terms( $post->ID, 'client_name' );
$slug = $filters[0]->slug . '/';
}
if ( isset( $slug ) ) {
$link = str_replace( $type, $slug, $link );
}
return $link;
}
add_filter( 'post_type_link', 'permalink_rewrites', 10, 2 );
Any suggestions on what I can do this fix this?
Thanks.