I have created a custom post type in WordPress which is working great. The only thing I need to solve are the permalinks created by this CTP.
I currently have:
example.com/<custom-post-name>/<post-id>/<postname>/
I want it to only display the post-id:
example.com/<custom-post-name>/<post-id>/
The custom post type is registered with:
function create_post_type_mycustomname() {
$args = array(
'capability_type' => 'post',
'has_archive' => 'mycustomname',
'rewrite' => array(
'slug' => '/mycustomname/%post_id%',
'feeds' => false
)
);
register_post_type('ctp_mycustomname', $args);
}
add_action('init', 'create_post_type_mycustomname');
And the %post_id
in the slug is getting replaced with the following:
function custom_post_mycustomname_link($post_link, $post = 0, $leavename = false) {
if($post->post_type == 'ctp_mycustomname') {
return str_replace('%post_id%', $post->ID, $post_link);
}
else {
return $post_link;
}
}
add_filter('post_type_link', 'custom_post_mycustomname_link', 1, 3);
Any hints on how to strip the postname from these kinds of URLs?