Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jstnbr/d2108f82e33baec013ddeca7563aa7b1 to your computer and use it in GitHub Desktop.
Save jstnbr/d2108f82e33baec013ddeca7563aa7b1 to your computer and use it in GitHub Desktop.
wp: CUSTOM TAXONOMY WITH SAME SLUG AS CUSTOM POST TYPE
<?php
/*
* Source: http://someweblog.com/wordpress-custom-taxonomy-with-same-slug-as-custom-post-type/
*/
// rewrite urls
function taxonomy_slug_rewrite($wp_rewrite) {
$rules = array();
$taxonomies = get_taxonomies(
array('_builtin' => false),
'objects'
);
$post_types = get_post_types(
array('public' => true, '_builtin' => false),
'names'
);
foreach ($post_types as $post_type) {
foreach ($taxonomies as $taxonomy) {
if ($taxonomy->object_type[0] != $post_type) continue;
$categories = get_categories(array(
'type' => $post_type,
'taxonomy' => $taxonomy->name,
'hide_empty' => 0
));
foreach ($categories as $category) {
$rules[$post_type . '/' . $category->slug . '/?$'] =
'index.php?' . $category->taxonomy . '=' . $category->slug;
}
} // taxonomies
} // post_types
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'taxonomy_slug_rewrite' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment