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'm trying to decide whether it would be more appropriate to create two new custom taxonomies or if custom page templates would be easier. Here's the scenario:

  • I have 2 custom posts types: "Cocktail Recipes" and "Distilleries"
  • I have roughly 10 terms in the custom taxonomy "Alcohol Spirits Types" and both custom post types use this taxonomy. The number of these terms is unlikely to grow.
  • I need 3 archive pages for each Alcohol Spirit Type term. One that displays results from both custom post types, one that displays results only from Cocktail Recipes and one that displays results only from Distilleries (e.g. Whiskey, Whiskey Cocktail Recipes and Whiskey Distilleries).

Should I: 1. Create two new custom taxonomies. One would be specific to Cocktail Recipes and the other specific to Distilleries, but both would have identical terms to the already created Alcohol Spirit Types taxonomy. I could then use the taxonomy-custom_taxonomy_name.php template to create the pages. Or... 2. Create two custom page templates (one for Alcohol Spirits in Cocktail Recipes and the other for Alcohol Spirits in Distilleries) and two pages for each Spirit Type (roughly 20 pages total).

To me, option 2 makes more sense because then I'm not duplicating terms in three different taxonomies. Also, a post editor would only need to tag a post twice. If the number of terms were larger this solution would be a huge task initially, but creating only 20 pages seems reasonable.

What do you think? Is there an easier way I'm not thinking of?

share|improve this question
add comment

1 Answer

You could do it in a more dynamic fashion, with a simple get var, or you could get complicated and create some rewrite rules to parse your own URLs.

Here's a quick and simple GET var method -

Say you have a taxonomy term at:

example.com/alcohol-spirit-type/gin/

This would show posts of both post types associated with the taxonomy term.

Now append a variable on the end for each of your post types to create the URLs you'll filter, like:

// change these to reflect the actual registered names of your post types

example.com/alcohol-spirit-type/gin/?my_filter=cocktail-recipe

and

example.com/alcohol-spirit-type/gin/?my_filter=distillery

Now add a bit of code to the theme's functions.php file to detect this appended variable and adjust the query accordingly:

function wpa54401_filter_pre_get_posts( $query ) {
    if ( isset( $_GET['my_filter'] ) ) {
        $query->set( 'post_type', array( $_GET['my_filter'] ) );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'wpa54401_filter_pre_get_posts' );

Now visiting each of those URLs should give you only posts within each type.

EDIT-

Here's a filter on taxonomy_template to return custom templates for the two new views:

function wpa54401_custom_taxonomy_template( $template ) {
    if ( isset( $_GET['my_filter'] ) ) {
        $template = dirname( __FILE__ ) . '/tax-' . $_GET['my_filter'] . '.php';
    }
    return $template;
}
add_filter( "taxonomy_template", "wpa54401_custom_taxonomy_template" ) ;

the templates should be named tax-{your post type}.php

share|improve this answer
    
Thanks for the help Milo. Unfortunately, I need to change more than just the query. Each page requires a different design. Is there a way to serve a different page template based on the variable passed? Also, it's important that the pages are indexed by search engines. What is the effect on SEO from using the get var? Is it indexable? –  Nick Jun 7 '12 at 20:09
    
@Nick - see edit for taxonomy template filter. the pages can be indexed separately, however, they'll likely be seen as subsets of the same content on the main page and it's not likely to help your rank. –  Milo Jun 8 '12 at 13:25
add comment

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.