Site icon Hip-Hop Website Design and Development

Add %post_id% to slug of customized submit sort and forestall the "unique slug" factor that WP does?

I’ve a customized submit sort referred to as “tools”. I would love the permalink construction to be:

instance.com/tools/%post_id%-slug

I particularly need it to make use of a touch and never a slash (i.e. – “id-slug” not “id/slug”). Each attainable resolution I’ve discovered from googling has used slashes and I simply cannot get it to work with a touch.

I additionally need to forestall WordPress from including on the extra quantity if there’s two posts with the identical title. Instance:

instance.com/tools/45-awesomeness
and
instance.com/tools/46-awesomeness-2

I assume the answer is to make use of add_rewrite_rule, however I am a bit confused with the regex stuff.

The next code did work to exchange the submit identify with the id within the slug:

operate equipment_links($post_link, $submit = 0) {
    if($post->post_type === 'tools') {
        return home_url('tools/' . $post->ID . '');
    }
    else{
        return $post_link;
    }
}
add_filter('post_type_link', 'equipment_links', 1, 3);


operate equipment_rewrites_init(){
    add_rewrite_rule('tools/([0-9]+)?$', 'index.php?post_type=tools&p=$matches[1]', 'prime');
}
add_action('init', 'equipment_rewrites_init');

It did return a url of “instance.com/tools/123”, however I wish to tack on “-post_name” to the tip of that “123”.