Add %post_id% to slug of custom post type and prevent the "unique slug" thing that WP does?
I have a custom post type called "equipment". I would like the permalink structure to be:
example.com/equipment/%post_id%-slug
I specifically want it to use a dash and not a slash (i.e. - "id-slug" not "id/slug"). Every possible solution I've found from googling has used slashes and I just can't get it to work with a dash.
I also want to prevent WordPress from adding on the additional number if there's two posts with the same title. Example:
example.com/equipment/45-awesomeness
and
example.com/equipment/46-awesomeness-2
I think the solution is to use add_rewrite_rule, but I'm a bit confused with the regex stuff.
The following code did work to replace the post name with the id in the slug:
function equipment_links($post_link, $post = 0) {
if($post-post_type === 'equipment') {
return home_url('equipment/' . $post-ID . '');
}
else{
return $post_link;
}
}
add_filter('post_type_link', 'equipment_links', 1, 3);
function equipment_rewrites_init(){
add_rewrite_rule('equipment/([0-9]+)?$', 'index.php?post_type=equipmentp=$matches[1]', 'top');
}
add_action('init', 'equipment_rewrites_init');
It did return a url of "example.com/equipment/123", but I would like to tack on "-post_name" to the end of that "123".
Topic url-rewriting urls slug custom-post-types Wordpress
Category Web