WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to adapt some code on calling a custom template via rewrite rule.

I have a custom taxonomy called "state". I want to create an archive for the taxonomy, that lists all the terms in that taxonomy.


Would load my custom template where I can query the terms and loop through them.

Alabama Alaska Arizone Arkinsas, etc

I know there is a possibility of some conflict if I had a page named "state" too, but that's a risk I can take.

The code in the tutorial works really well for

http://www.site.com/state/1 or http://www.site.com/state/2 but returns a 404 if there is no number captured.

and that number would be good for pagination if the terms list gets out of control.

Here's the code:

add_action('admin_init', 'handle_taxonomy_route');
function handle_taxonomy_route()
    add_rewrite_rule('^state/([^/]+)/?', 'index.php?state_var=$matches[1]', 'top');

add_filter('init', 'declare_vars');
function declare_vars()
    add_rewrite_tag('%state_var%', '([^&]+)');

add_filter('template_include', 'my_template', 1, 1);
function my_template($template)
    global $wp_query;

    if (isset($wp_query->query_vars['state_var'])) {
        return dirname(__FILE__) . '/state-list.php';
    return $template;

From what I have tried so far, it doesn't appear to conflict with


Where Alabama is a term in the state taxonomy, but it should definitely only capture numbers.

A similar question went unanswered last year. How do I list terms of a custom taxonomy at i.e. domain.com/brands/

share|improve this question

The 404 might be created because your regular expression requires the value after 'state/'. Please try to replace:

function handle_taxonomy_route()
    add_rewrite_rule('^state/([^/]+)/?', 'index.php?state_var=$matches[1]', 'top');


function handle_taxonomy_route()
    add_rewrite_rule('^state/([^/]*)/?', 'index.php?state_var=$matches[1]', 'top');
share|improve this answer
This doesn't seem to make a difference. state_var is not added so the template_include function isn't finding it. I did a var_dump($wp_query) and it looks like I might be able to make it work with the pagename parameter. WordPress is looking at my URL like it thinks it is a page. – helgatheviking Mar 6 '13 at 0:45
up vote 0 down vote accepted

I needed a better rewrite rule for proper pagination.

function kia_custom_rewrite() {

    // registers the state_var query variable

    // handles site.com/state and pagination such as site.com/state/page/2

// refresh/flush permalinks in the dashboard if this is changed in any way
add_action( 'init', 'kia_custom_rewrite', 99 );

The rewrite rule adds the query_var to check for here:

 * Load custom templates
 * 1. Get a custom template for the /state "page"
 * 2. Get a custom template for the /state/some-state term
function kia_template( $template ){
    global $wp_query;

    if ( 'all' == get_query_var('state_var') ) {
        $template = get_stylesheet_directory() . '/taxonomy-state-list.php';

    return $template;
add_filter( 'template_include', 'kia_template' );
share|improve this answer

Your Answer


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.