Forked from fomigo/taxonomy-slug-as-custom-post-type.php
Last active
April 10, 2018 08:16
-
-
Save gagimilicevic/49bed5398dd32dbbc130d8cd6ba1c0de 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; | |
$rules[$post_type . '/' . $term->slug . '/page/?([0-9]{1,})/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug . '&paged=' . $wp_rewrite->preg_index( 1 ); | |
} | |
} // 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