Mixing custom post type and taxonomy rewrite structures?

You can always override the template that will be called with the template_include or a related filter, but this might hide deeper problems with custom archives.

As I understand it, you want to use the following structure:

  • /glossary/ should be an archive page for all sumo-glossary-term posts
  • /glossary/[letter]/ should be an archive page for posts with the taxonomy term [letter] in the sumo-glossary-letter taxonomy
  • /glossary/[letter]/[term-name]/ should be an individual sumo-glossary-term post

This means that the first will load the template archive-sumo-glossary-term.php, the second will load taxonomy-sumo-glossary-letter.php, and the third will load single-sumo-glossary-term.php.

I achieved this in WordPress 3.2 by explicitly setting the taxonomy rewrite slug, and both the rewrite slug and the archive slug for the post type, and no other rewrite rules. Also, I registered the taxonomy first and the post type after it, to make sure the priorities were right (otherwise a URL like /glossary/f/page/2 goes to glossary term page instead of page 2 of glossary letter f).

add_action('init', 'create_glossary');
function create_glossary()
{

    register_taxonomy
    (
        'sumo-glossary-letter',
        array(),
        array
        (
            'hierarchical' => true,
            'labels' => array
            (
                'name' => _x('Letters', 'taxonomy general name'),
                'singular_name' => _x('Letter', 'taxonomy singular name')
                # And so one
            ),
            'show_ui' => true,
            'query_var' => 'glossary-letter',
            'rewrite' => array(
                'slug' => 'glossary',
            ),
        )
    );

    register_post_type
    (
        'sumo-glossary-term',
        array
        (
            'labels' => array
            (
                'name' => _x('Glossary Terms', 'post type general name'),
                'singular_name' => _x('Glossary Term', 'post type singular name')
                # And so on …
            ),
            'supports' => array('title', 'editor', 'thumbnail'),
            'public' => true,
            'rewrite' => array
            (
                'slug' => 'glossary/%sumo-glossary-letter%',
                'with_front' => false

            ),
            'query_var' => 'glossary-term',
            'has_archive' => 'glossary',
            'taxonomies' => array( 'sumo-glossary-letter' ),
        )
    );
}

add_filter('post_type_link', 'glossary_term_permalink', 10, 4);
function glossary_term_permalink($post_link, $post, $leavename, $sample)
{
    if ( false !== strpos( $post_link, '%sumo-glossary-letter%' ) ) {
        $glossary_letter = get_the_terms( $post->ID, 'sumo-glossary-letter' );
        $post_link = str_replace( '%sumo-glossary-letter%', array_pop( $glossary_letter )->slug, $post_link );
    }
    return $post_link;
}

Leave a Comment