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.

So, I've been going crazy trying to figure this out. Essentially, I've created two custom post types, 'the_case_study' and 'the_video', with respective slugs 'case-studies' and 'videos'.

I've also created three taxonomies which apply to both the_case_study and the_video. These are 'the_room', 'the_system' and 'the_style'.

By default, the taxonomy archive displays all posts tagged with a given term, and the permalink looks like this:

example.com/room/kitchen (displays all posts tagged with kitchen)

I would like to have a taxonomy archive that will only display the posts of the current custom post type and will have a permalink that is relative to that custom post type. For example:

example.com/case-studies/room/kitchen (should display all case studies tagged with kitchen)

example.com/videos/room/kitchen (should display all videos tagged with kitchen)

I've already resolved a similar problem elsewhere on the site, based on this answer among other things. However, I only know how to make that work for one post type.

This seems like something that should be possible, but I don't know how to go about it. Essentially, I'd like to be able to find the parent of the current taxonomy page, where that parent is the current custom post type... but I'm not sure how that jives with the custom post type and taxonomy constraints.

Is what I'm trying to do possible? Should I be thinking about it differently?

share|improve this question

2 Answers 2

You could alter the query directly in your template or use the pre_get_posts action.

Inside archive template:

$taxonomies_to_match = array('the_room','the_system','the_style');
if(is_tax($taxonomies_to_match)) {
  global $wp_query;
  query_posts(array_merge($wp_query->query_vars,array('post_type' => 'the_case_study'));
}

Action Hook Method:

add_action('pre_get_posts','my_post_filter');
function my_post_filter($query) {
  $taxonomies_to_match = array('the_room','the_system','the_style');
  if(is_tax($taxonomies_to_match)) {
    $query->set('post_type','the_post_type_you_want');
  }
}
share|improve this answer

This is very easy to do by adding custom rewrite rules. This should get you started:

add_action( 'init', 'wpse_100386_rewrites' );
function wpse_100386_rewrites() {
    add_rewrite_rule( 'case-studies/room/([^/]+)/?', 'index.php?the_room=$matches[1]&post_type=the_case_study', 'top' );
    add_rewrite_rule( 'videos/room/([^/]+)/?', 'index.php?the_room=$matches[1]&post_type=the_video', 'top' );
}

As always when changing rewrite rules, be sure to flush your rewrite rules by going to Settings -> Permalinks and click "Save Changes".

share|improve this answer

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.