For a custom post type blog in my site, I want to have the following permalink structure: mysite.com/blog/post-slug/post-id I am using the following code in my theme:

 function blog_links($post_link, $post = 0) {
    if($post->post_type === 'blog') {
        return home_url('blog/' . $post->post_name . '/' . $post->ID . '');
        return $post_link;
add_filter('post_type_link', 'blog_links', 1, 3);
function blog_rewrites_init(){
    add_rewrite_rule('blog/([0-9]+)?$', 'index.php?post_type=blog&p=$matches[1]', 'top');
add_action('init', 'blog_rewrites_init'

However, the post is giving 404 error. Kindly help with correct rewriterule or code. Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.