Wasn’t as hard as I thought. Here’s the working example, in case anyone finds it helpful:
function em_query_vars($vars) {
array_push($vars, 'state');
array_push($vars, 'city');
return $vars;
}
add_filter('query_vars','em_query_vars');
function em_rewrite() {
add_rewrite_rule( '^state/([^/]*)/([^/]*)/?', 'index.php?state=$matches[1]&city=$matches[2]', 'top' );
add_rewrite_rule( '^state/([^/]*)/?', 'index.php?state=$matches[1]', 'top' );
add_rewrite_tag('%state%','([^&]+)');
add_rewrite_tag('%city%','([^&]+)');
}
add_action('init', 'em_rewrite');
function em_templates($template) {
global $wp_query;
if (isset($wp_query->query_vars['state']) && isset($wp_query->query_vars['city'])) {
return dirname(__FILE__) . '/single-city.php';
} elseif (isset($wp_query->query_vars['state'])) {
return dirname(__FILE__) . '/single-state.php';
}
return $template;
}
add_filter('template_include', 'em_templates', 1, 1);