Created
July 16, 2015 13:56
-
-
Save will83/9f173d096db78fc17342 to your computer and use it in GitHub Desktop.
Rewrite Custom Taxonomies Routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Replace Taxonomy slug with Post Type slug in url | |
* Version: 1.1 | |
*/ | |
function ss_fix_portfolio_rewrite($wp_rewrite) { | |
$rules = array(); | |
// get all custom taxonomies | |
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects'); | |
// get all custom post types | |
$post_types = get_post_types(array( | |
'public' => true, | |
'_builtin' => false), | |
'objects'); | |
foreach ($post_types as $post_type) { | |
$post_type_name = $post_type->name; | |
$post_type_slug = $post_type->name; | |
if ( $post_type->rewrite && $post_type->rewrite['slug'] ){ | |
$post_type_slug = $post_type->rewrite['slug']; | |
} | |
foreach ($taxonomies as $taxonomy) { | |
// check if taxonomy is registered for this custom type | |
if ($taxonomy->object_type[0] == $post_type_name) { | |
// get all categories (terms) | |
$categories = get_categories(array( | |
'type' => $post_type_name, | |
'taxonomy' => $taxonomy->name, | |
'hide_empty' => 0 | |
)); | |
// make rules | |
foreach ($categories as $category) { | |
$rules[$post_type_slug . '/' . $category->slug . '/?$'] = | |
'index.php?' . $category->taxonomy . '=' . $category->slug; | |
} | |
} | |
} | |
} | |
// merge with global rules | |
$wp_rewrite->rules = $rules + $wp_rewrite->rules; | |
} | |
add_filter('generate_rewrite_rules', 'ss_fix_portfolio_rewrite'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment