2

I’m looking for a way to solve my problem with permalinks in wordpress.

I have 3 post types: news (default), posttype1, posttype2

all 3 uses the standard category. I added 3 categories in it: cat1, cat2, cat3

What I want is this:

www.mywebsite.com/posttype1 : show all posts of « posttype1 »

www.mywebsite.com/posttype1/cat1 : show all posts of « posttype1 » that are in category « cat1 »

www.mywebsite.com/posttype1/cat1/post-name : show the post named « post-name » in « posttype1 » that is in category « cat1 »

and apply that rule to all 3 post types… I’ve tried almost everything about rewriting rules in function.php, but can’t find any solution. And if I put /%category%/%postname%/ in permalink settings, it actually works well for the default post type, but don’t work for both custom post types: the category is not taken into account.

I’ve tried to install the Custom Post Type Permalinks plugin, but here again the category is not taken into account.

This is actually what I have in my functions.php (for posttype1)

register_post_type(‘posttype1’, array(
  'label' => __(‘Posttype1’),
  'singular_label' => __(‘Posttype1’),
  'public' => true,
  'show_ui' => true,
  'capability_type' => 'post',
  'hierarchical' => true,
  'menu_position' => 4,
  'supports' => array('title', 'editor'),
  'rewrite' => array('slug' => 'posttype1', 'with_front' => false),
  'has_archive' => true,
  'taxonomies' => array('category')
));
1

There are 3 parts to making this work.

Register the post type with correct rewrite slug and archive.

We add the %category% rewrite tag to the slug so we can swap in the selected category. We also specify the archive name explicitly. I've omitted the rest of the register_post_type arguments here, the rest can be as-is in your own example.

'rewrite' => array('slug' => 'posttype1/%category%', 'with_front' => false),
'has_archive' => 'posttype1',

Add a rewrite rule to handle post type / category archive.

add_rewrite_rule(
    'posttype1/([^/]+)/?$',
    'index.php?post_type=posttype1&category_name=$matches[1]',
    'top'
);

This will enable category archive views for your post type. Place both of the above in a function hooked to init.

Filter post_type_link to swap in the category name for post permalinks.

function wpd_custom_post_link( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) && $post->post_type == 'posttype1' ){
        $terms = wp_get_object_terms( $post->ID, 'category' );
        if( $terms ){
            return str_replace ( '%category%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpd_custom_post_link', 1, 3 );

Repeat the above steps for each post type you want to enable.

  • Thanks for your answer. I've tried this solution but I still have a problem. www.mywebsite.com/posttype1 actually shows all posts of « posttype1 », but if I put this url: www.mywebsite.com/posttype1/cat1/, it also shows all posts of "posttype1", without taking "cat1" in account... What should I put as default permalink options in Wordpress settings? – theplayer777 Apr 13 '14 at 23:12
  • after you add rewrite rules and post types, you have to flush rewrite rules. you can do this by visiting your permalinks settings page. the above is separate from the permalink settings you set for standard posts, as long as some form of pretty permalink is working, it should work. you can try to swap the order of post type registration and rewrite rule, if it's still not working. – Milo Apr 13 '14 at 23:40
  • I have found my error... in my archive-posttype.php, my query was returning ALL the posts. Now it works like a charm! Thanks for your help!! – theplayer777 Apr 14 '14 at 0:04

Your Answer

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

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