#19275 closed defect (bug) (fixed)
Rewrite endpoints don't work with taxonomies
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 3.4 |
| Component: | Permalinks | Version: | 3.1 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | bill.erickson@…, gabriel.koen@…, ben@…, dempsey@…, marko@… |
Description
Adding a rewrite endpoint on category archives apparently confuses WP_Query and results in 404 errors. That is, according to all the documentation I have found on WP-Rewrite, this should work:
http://example.com/category/slug/format/myformat/
But instead, you have to use:
http://example.com/category/slug/?format=myformat
Excerpt from a sample plugin:
// Add permalink structure, flush rules on activation/deactivation
function uwc_ebook_activate() {
uwc_ebook_add_rewrites();
flush_rewrite_rules();
}
function uwc_ebook_deactivate() {
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'uwc_ebook_activate');
register_deactivation_hook(__FILE__, 'uwc_ebook_deactivate');
// Add rewrite endpoint
function uwc_ebook_add_rewrites() {
add_rewrite_endpoint('format', EP_CATEGORIES);
// this doesn't work either:
// add_rewrite_endpoint('format', EP_ALL);
}
add_filter('init', 'uwc_ebook_add_rewrites');
// Handle the endpoint display
function uwc_ebook_template_redirect() {
// global $wp_query; var_dump($wp_query); exit;
$format = get_query_var('format');
$cat = get_query_var('cat');
if ($format == 'epub' && isset($cat)) {
$category = get_category($cat);
if (!is_wp_error($category)) {
$uploads = wp_upload_dir();
$ebookurl = $uploads['baseurl'].'/ebooks/'.$category->slug.'/'.$category->slug.'.epub';
wp_redirect( $ebookurl, 301 );
exit;
}
}
}
add_filter('template_redirect', 'uwc_ebook_template_redirect');
// the rest of the plugin generates the epub file containing all posts in that category
Uncommenting the $wp_query line shows what's going on. Relevant parts of the var dump, from the Science & Technical subcategory of How-To:
["category_name"]=>
string(4) "epub"
["query"]=>
array(1) {
["category_name"]=>
string(36) "how-to/science-technical/format/epub"
}
Using /how-to/science-technical/?format=epub instead, WP correctly recognizes the category and format as two different query vars.
I tested this in the latest nightly build, and it's broken there as well.
Bill Erickson had a similar problem a few weeks ago, in which he used EP_ALL and found that it worked everywhere except category archives:
https://gist.github.com/1287996
Attachments (1)
Change History (16)
comment:1
billerickson — 18 months ago
- Cc bill.erickson@… added
- Summary changed from Rewrite endpoints don't work with category archives; added query vars treated as category slugs to Rewrite endpoints don't work with taxonomies
19275.diff standardizes on ep_mask being an optional argument for $argsrewrite? for either taxonomies or post types, and makes endpoints work for categories and tags.
comment:7
mintindeed — 18 months ago
- Cc gabriel.koen@… added
- Owner set to nacin
- Resolution set to fixed
- Status changed from new to closed
In [19738]:
comment:10
nacin — 16 months ago
In [19740]:
comment:11
nacin — 16 months ago
comment:12
nacin — 16 months ago
In [19741]:
comment:13
husobj — 16 months ago
- Cc ben@… added
comment:14
dempsey — 15 months ago
- Cc dempsey@… added
comment:15
markoheijnen — 14 months ago
- Cc marko@… added
Just curious how does this effect #12779 (better support for custom post types in WP_Rewrite).

The issue is a change in 3.1, which made taxonomies all be handled like any other permastruct. This means the endpoints are no longer properly checked. Surprised it was missed until now.
Unlike register_post_type(), register_taxonomy() does not provide a way to specify an endpoint. So fixing this also requires an API enhancement.