Last active
October 10, 2017 18:00
-
-
Save treighton/f7decd83fc9d05fc099b0e56e75d711f to your computer and use it in GitHub Desktop.
Rewrite WP Taxonomy URLS
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 | |
function custom_post_type() { | |
$labels = array( | |
'name' => _x( 'Resources', 'Resource General Name', 'namspace' ), | |
'singular_name' => _x( 'Resource', 'Resource Singular Name', 'namspace' ), | |
'menu_name' => __( 'Resources', 'namspace' ), | |
'name_admin_bar' => __( 'Resource', 'namspace' ), | |
'archives' => __( 'Resource Archives', 'namspace' ), | |
'attributes' => __( 'Resource Attributes', 'namspace' ), | |
'parent_item_colon' => __( 'Parent Resource:', 'namspace' ), | |
'all_items' => __( 'All Resources', 'namspace' ), | |
'add_new_item' => __( 'Add New Resource', 'namspace' ), | |
'add_new' => __( 'Add New', 'namspace' ), | |
'new_item' => __( 'New Resource', 'namspace' ), | |
'edit_item' => __( 'Edit Resource', 'namspace' ), | |
'update_item' => __( 'Update Resource', 'namspace' ), | |
'view_item' => __( 'View Resource', 'namspace' ), | |
'view_items' => __( 'View Resources', 'namspace' ), | |
'search_items' => __( 'Search Resource', 'namspace' ), | |
'not_found' => __( 'Not found', 'namspace' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'namspace' ), | |
'featured_image' => __( 'Featured Image', 'namspace' ), | |
'set_featured_image' => __( 'Set featured image', 'namspace' ), | |
'remove_featured_image' => __( 'Remove featured image', 'namspace' ), | |
'use_featured_image' => __( 'Use as featured image', 'namspace' ), | |
'insert_into_item' => __( 'Insert into item', 'namspace' ), | |
'uploaded_to_this_item' => __( 'Uploaded to this item', 'namspace' ), | |
'items_list' => __( 'Resources list', 'namspace' ), | |
'items_list_navigation' => __( 'Resources list navigation', 'namspace' ), | |
'filter_items_list' => __( 'Filter items list', 'namspace' ), | |
); | |
$args = array( | |
'label' => __( 'Resource', 'namspace' ), | |
'description' => __( 'Resource Description', 'namspace' ), | |
'rewrite' => array( 'slug' => 'resources' ), | |
'labels' => $labels, | |
'supports' => array( ), | |
'taxonomies' => array( 'resource_type' ), | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'show_in_admin_bar' => true, | |
'show_in_nav_menus' => true, | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'capability_type' => 'page', | |
'show_in_rest' => true, | |
); | |
register_post_type( 'resources', $args ); | |
} | |
add_action( 'init', 'custom_post_type', 0 ); | |
function resource_taxonomy() { | |
$labels = array( | |
'name' => _x( 'Resource Types', 'Taxonomy General Name', 'namspace' ), | |
'singular_name' => _x( 'Resource Type', 'Taxonomy Singular Name', 'namspace' ), | |
'menu_name' => __( 'Resource Type', 'namspace' ), | |
'all_items' => __( 'All Resource Types', 'namspace' ), | |
'parent_item' => __( 'Parent Resource Type', 'namspace' ), | |
'parent_item_colon' => __( 'Parent Resource Type:', 'namspace' ), | |
'new_item_name' => __( 'New Resource Type Name', 'namspace' ), | |
'add_new_item' => __( 'Add New Resource Type', 'namspace' ), | |
'edit_item' => __( 'Edit Resource Type', 'namspace' ), | |
'update_item' => __( 'Update Resource Type', 'namspace' ), | |
'view_item' => __( 'View Resource Type', 'namspace' ), | |
'separate_items_with_commas' => __( 'Separate items with commas', 'namspace' ), | |
'add_or_remove_items' => __( 'Add or remove items', 'namspace' ), | |
'choose_from_most_used' => __( 'Choose from the most used', 'namspace' ), | |
'popular_items' => __( 'Popular Resource Types', 'namspace' ), | |
'search_items' => __( 'Search Resource Types', 'namspace' ), | |
'not_found' => __( 'Not Found', 'namspace' ), | |
'no_terms' => __( 'No items', 'namspace' ), | |
'items_list' => __( 'Resource Types list', 'namspace' ), | |
'items_list_navigation' => __( 'Resource Types list navigation', 'namspace' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => true, | |
'public' => true, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
'show_in_rest' => true, | |
'rewrite' => array( 'slug' => 'resources' ), | |
'rest_base' => 'resource_type', | |
); | |
register_taxonomy( 'resource_type', array( 'resources' ), $args ); | |
} | |
add_action( 'init', 'resource_taxonomy', 0 ); | |
function generate_taxonomy_rewrite_rules( $wp_rewrite ) { | |
$rules = array(); | |
$post_types = get_post_types( array( 'name' => 'resources', 'public' => true, '_builtin' => false ), 'objects' ); | |
$taxonomies = get_taxonomies( array( 'name' => 'resource_type', 'public' => true, '_builtin' => false ), 'objects' ); | |
foreach ( $post_types as $post_type ) { | |
$post_type_name = $post_type->name; // 'developer' | |
$post_type_slug = $post_type->rewrite['slug']; // 'developers' | |
foreach ( $taxonomies as $taxonomy ) { | |
if ( $taxonomy->object_type[0] == $post_type_name ) { | |
$terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) ); | |
foreach ( $terms as $term ) { | |
$rules[$post_type_slug . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug; | |
} | |
} | |
} | |
} | |
$wp_rewrite->rules = $rules + $wp_rewrite->rules; | |
} | |
add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment