Forked from fomigo/taxonomy-slug-as-custom-post-type.php
Created
November 5, 2022 20:05
-
-
Save jstnbr/d2108f82e33baec013ddeca7563aa7b1 to your computer and use it in GitHub Desktop.
wp: CUSTOM TAXONOMY WITH SAME SLUG AS CUSTOM POST TYPE
This file contains hidden or 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 | |
/* | |
* 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