Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active February 21, 2024 21:31
Show Gist options
  • Save igorbenic/f3d66cefaf1e6800ef1bee7ad8b25854 to your computer and use it in GitHub Desktop.
Save igorbenic/f3d66cefaf1e6800ef1bee7ad8b25854 to your computer and use it in GitHub Desktop.
Custom WordPress Rewrite Rule to Combine Taxonomy and Post Type | www.ibenic.com/custom-wordpress-rewrite-rule-combine-taxonomy-post-type
<?php
add_action('init', 'cpt_resources');
function cpt_resources() {
$labels = array(
'name' => _x('Resources', 'snt'),
'singular_name' => _x('Resource', 'snt'),
'add_new' => _x('Add Resource', 'snt'),
'add_new_item' => __('Add Resource'),
'edit_item' => __('Edit Resource'),
'new_item' => __('New Resource'),
'view_item' => __('View Resource'),
'search_items' => __('Search Resources'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'taxonomies' => array('resource_type'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','thumbnail', 'editor' ),
);
register_post_type( 'resources_post_type' , $args );
}
<?php
function resources_cpt_generating_rule($wp_rewrite) {
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'resource_type',
'hide_empty' => false,
) );
$post_type = 'resources_post_type';
foreach ($terms as $term) {
$rules['resources/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&resources_post_type=$matches[1]&name=$matches[1]';
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule');
<?php
function resource_type() {
$labels = array(
'name' => _x( 'Resource Types', 'Taxonomy General Name', 'snt' ),
'singular_name' => _x( 'Resource Type', 'Taxonomy Singular Name', 'snt' ),
'menu_name' => __( 'Resource Types', 'snt' ),
'all_items' => __( 'All Items', 'snt' ),
'parent_item' => __( 'Parent Item', 'snt' ),
'parent_item_colon' => __( 'Parent Item:', 'snt' ),
'new_item_name' => __( 'New Item Name', 'snt' ),
'add_new_item' => __( 'Add New Item', 'snt' ),
'edit_item' => __( 'Edit Item', 'snt' ),
'update_item' => __( 'Update Item', 'snt' ),
'view_item' => __( 'View Item', 'snt' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'snt' ),
'add_or_remove_items' => __( 'Add or remove items', 'snt' ),
'choose_from_most_used' => __( 'Choose from the most used', 'snt' ),
'popular_items' => __( 'Popular Items', 'snt' ),
'search_items' => __( 'Search Items', 'snt' ),
'not_found' => __( 'Not Found', 'snt' ),
'no_terms' => __( 'No items', 'snt' ),
'items_list' => __( 'Items list', 'snt' ),
'items_list_navigation' => __( 'Items list navigation', 'snt' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'resources')
);
register_taxonomy( 'resource_type', array( 'resources_post_type' ), $args );
}
add_action( 'init', 'resource_type', 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment