Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have been digging the code for days and no solution. :( I have a Custom Post Type and two different Taxonomies.

It is possible to achieve the following permalink structure (with add_rewrite_rule, mod_rewrite or post_type_link post type filter) or not?

mysite.com/post-type-slug/post1
mysite.com/post-type-slug/taxonomy1-term
mysite.com/post-type-slug/taxonomy2-term

not required, but would be nice to create these URLs too

mysite.com/post-type-slug/taxonomy1-term/taxonomy1-subterm - if each tax. is hierarchical
mysite.com/post-type-slug/taxonomy-term1/post1 - display posts under tax. URLs

Do you have any idea?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I'll look into the last two soon but for the top 3 you could do the following:

mysite.com/post-type-slug/post1:

This defaults to the post type name you specify when registering it but can be altered by changing the rewrite argument in the register_post_type() call.

register_post_type( 'posttype', array(
    // ... all the other args...
    'rewrite' => array( 'slug' => 'post-type-slug' ),
    // if you want to list a post type with no taxonomy involvement
    'has_archive' => 'post-type-slug'
) );

mysite.com/post-type-slug/taxonomy1-term:

I don't think you can easily get the hyphen between the taxonomy name and the term name if that's what you mean but you can do the following:

register_taxonomy( 'taxonomy1', 'posttype', array(
    // ... all the other args...
    'rewrite' => array( 'slug' => 'post-type-slug/taxonomy-slug' )
) );

Which will give you a URL like this:

mysite.com/post-type-slug/taxonomy-slug/term

I wasn't too sure from your question but if you don't want the taxonomy-slug to be there then you're out of luck, you can't have the same slug as your post type or another taxonomy as it won't know what term is from which taxonomy and what's a post.

To get the heirarchical terms URL do this:

register_taxonomy( 'taxonomy1', 'posttype', array(
    // ... all the other args...
    'rewrite' => array( 'slug' => 'post-type-slug/taxonomy-slug', 'heirarchical' => true )
) );

Which gives you

mysite.com/post-type-slug/taxonomy-slug/term/sub-term

Note:

If it helps setting slug to false gets rid of it altogether so you'd get:

mysite.com/term/sub-term

share|improve this answer
    
Thank you for your answer. The following structure what you mentioned is good for me: mysite.com/post-type-slug/taxonomy-slug/term A quite strange thing happened. :) This solution only works with hierarchical taxonomies. more accurately only taxonomy-sub-term URLs work. mysite.com/post-type-slug/taxonomy1-slug/taxonomy1-term/taxonomy1-subterm - works mysite.com/post-type-slug/taxonomy1-slug/taxonomy1-term - doesn't work (404) –  user9909 Nov 3 '11 at 19:37
    
@user9909 is that with 'hierarchical' => true in the rewrite argument of register taxonomy? You may have to flush the permalinks first by going to permalinks in the admin and clicking save changes. –  sanchothefat Nov 4 '11 at 9:46
    
I changed the order of Taxonomies and CPTs. It works. Great. :D –  user9909 Nov 4 '11 at 9:51
    
Ah cool. Glad I could help. Unfortunately the more advanced single post page URLs are a lot trickier to do. I'll post here anyway if I get a chance to figure it out. –  sanchothefat Nov 4 '11 at 10:04
    
Thnak you. I tried to manage with a post_type_link filter, but couldn't. It's enough for me, that would be onlye a plus. :) –  user9909 Nov 5 '11 at 8:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.