Mixing custom post type and taxonomy rewrite structures?

Basically I want to achieve a glossary using custom post types and have some issues setting up rewrites the way I want them to be. I want it like that:

The main glossary URL:
http://example.com/glossary/

Glossary terms starting with letter A:
http://example.com/glossary/a/

The URL for a single glossary term:
http://example.com/glossary/a/atomic/

I actually achieved this using the code below but I’m sure that it’s a very awkward way to do it and I know it’s malfunctioning somewhere as the wrong templates are called when viewing the pages. Except for http://example.com/glossary/, where archive-sumo-glossary-term.php gets called as expected, the other two just activate index.php in my theme.

Here goes (functions.php in the theme):

add_action('init', 'create_glossary');
function create_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',
                'with_front' => false

            ),
            'query_var' => 'glossary-term',
            'has_archive' => true
        )
    );

    register_taxonomy
    (
        'sumo-glossary-letter',
        'sumo-glossary-term',
        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' => false
        )
    );
}

add_filter('post_type_link', 'glossary_term_permalink', 10, 4);
function glossary_term_permalink($post_link, $post, $leavename, $sample)
{
    if ($post->post_type == 'sumo-glossary-term')
    {
        $permalink = str_replace('glossary/', 'glossary/' . $post->post_name[0] . "https://wordpress.stackexchange.com/", $post_link);
    }
    return $permalink;
}

add_rewrite_rule('^glossary/([^/]*)?$','index.php?glossary-letter=$matches[1]','top');
add_rewrite_rule('^glossary/([^/]*)/([^/]*)?$','index.php?glossary-term=$matches[2]','top');

2

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