0

This can solve my problem but after many attempts I am not able to do it. I tried adding /%book-category%/%post_name%/ to permalink but doesn't work.

I am trying to achieve books/story-books/story-1...

What could I be missing?

CPT- books

CPT Taxonomy- book-category

My Code goes here-

add_filter('post_link', 'books_permalink', 1, 3); add_filter('post_type_link', 'books_permalink', 1, 3);

function books_permalink($permalink, $post_id, $leavename) {

    if (strpos($permalink, '%book-category%') === FALSE) return $permalink;
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'book-category');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
          $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'book';

    return str_replace('%book-category%', $taxonomy_slug, $permalink);
}

This is digging deep into the wordpress and to my previous question.

  • 2
    Possible duplicate of Permalinks: custom post type -> custom taxonomy -> post – Milo Jan 22 '17 at 16:07
  • @Milo ofcourse it is duplicate but I want to have solution. – Vishal Kumar Sahu Jan 22 '17 at 16:38
  • 1
    Well have you tried the code from that answer? – Milo Jan 22 '17 at 17:09
  • Hi I am trying but currently I am getting 404 error. That question is well explained. I tried many other answers but that answer is actually to the point for me. :) Thank you. – Vishal Kumar Sahu Jan 22 '17 at 17:22
  • I am confident that I will get it done now as I have got the key to do it... :) Thanks a lot @Milo – Vishal Kumar Sahu Jan 22 '17 at 17:22

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.