I'm sorry if the post title is not too understandable...
I have a custom post type called houses and I rewrite dynamically the slug depending on the taxonomy term. The taxonomy is called sizes and the terms are big and small.
When I create 2 posts with same title, the output of the slugs is like this even though the rewrite function makes it to be different slugs:
- houses/big/red-house
- houses/small/red-house-2
I even attempted to change manually the slug on the edit option but it does not let me, it still leaves the -2 at the end of the slug. The same goes for the database, it redirects to the first post....
I don't understand why wordpress sees them as same slug... is not identifying the rewrite?
Here how I created the custom post type:
$labels = array(
'name' => 'Houses',
'singular_name' => 'House',
'menu_name' => 'Houses',
'search_items' => 'Houses'
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'menu_icon' => 'dashicons-admin-home',
'supports' => array('title', 'editor'),
'taxonomies' => array('sizes', 'post_tag'), // Term & Tag
'has_archive' => false,
'rewrite' => array( 'slug' => 'houses/%sizes%'),
'query_var' => true
);
register_post_type('houses', $args);
Here is my taxonomy code:
$labels = array(
'name' => 'Sizes',
'singular_name' => 'Size',
'menu_name' => 'Sizes',
);
$args = array(
'labels' => $labels, // display name
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'with_front' => true
);
register_taxonomy( 'sizes', 'houses', $args); // Slug, CPT, array
And this is the code I'm using to dynamically change the slugs:
function houses_link( $post_link, $id = 0 )
{
$post = get_post($id);
if ( is_object( $post ) )
{
$terms = wp_get_object_terms( $post->ID, 'houses' );
if( $terms )
{
return str_replace( '%sizes%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'houses_link', 1, 3 );
I hope that my explanation was clear :') thanks for the help!