So this is driving me absolutely insane. I’ve spend days trying to fix this and I can’t work out why this is so difficult as surely this is a very common permalink structure!
I looked through hundreds of answers and posts and none of them seem to solve the problem.
I simply want this structure:
mysite.com/custom-post-type/custom-taxonomy-term/post-name
So I achieve the following:
mysite.com/literature - all literature posts
mysite.com/literature/fiction - all literature posts with 'fiction' term
mysite.com/literature/fiction/mybook - the post
Every time I try something I get 404 errors or the pagnination doesn’t work.
I don’t understand why this is so hard!
Any help really appreciated!
Thank you
======================================================
==================ADDITIONAL INFORMATION==================
Currently I’m registering the post type and taxonomy as follows:
register_post_type('literature',$args);
'rewrite' => array('slug' => 'literature/books','with_front' => false),
register_taxonomy('literature_category',array('literature'), array(
'rewrite' => array( 'slug' => 'literature','with_front' => false ),
If I register both as ‘literature’ I get a 404 on the mysite.com/literature page but this displays my permalink as: mysite.com/literature/books/mybook
After following the advice on this question – Custom post types, taxonomies, and permalinks
I’ve added this to my functions:
function filter_post_type_link($link, $post) {
if ($post->post_type != 'literature')
return $link;
if ($cats = get_the_terms($post->ID, 'literature_category'))
$link = str_replace('%literature_category%', array_pop($cats)->slug, $link);
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
And changed my post type to 'slug' => 'literature/%literature_category%'
And changed my taxonomy to 'slug' => 'literature'
This works great except that on mysite.com/literature
pagination doesn’t work so I get a 404 error on the following url:
mysite.com/literature/page/2/