Use same slug base for Custom Post Type posts, and multiple taxonomy terms
I have done a lot of digging the past few days, and can come close, but not exactly what I need.
I’m trying to alter a custom taxonomy/CPT permalink structure to all use the same slug base: /resources/.
CPT: 'resources'
TAX1, non-hierarchical: 'type'
TAX2, hierarchical: 'section'
I need the structure to look like this:
/resources/ = static page with blocks WORKS
/resources/$type-term/ = archive for all Resources posts with this term DOESN’T WORK – 404
/resources/$type-term/page/1 = pagination for type-term archive DOESN’T WORK
/resources/$section-term-parent/ = static page with blocks WORKS
/resources/$section-term-parent/$section-term-child/ = archive for all Resources posts with this child term WORKS
/resources/$section-term-parent/$section-term-child/page/2 = pagination for section-child-term archive WORKS
/resources/$post-name = url for all Resources posts DOESN’T WORK – 404
I need to remove the taxonomy names from both taxonomies, and use only the term name. Both taxonomies need to have /resources/ before them in the URL. The single posts need /resources/ before the post slug. It needs to be set at the WP level and not .htaccess because we have a load balancer directing all non-resources traffic to another system. If it’s not in /resources/ it will never hit WP.
I can only get one taxonomy at a time to use the structure, but not both. Whichever taxonomy I declare second is the one that works. I am using %category%/%postname% in my global permalink rules.
Registering content types:
add_action( 'init', 'create_format_taxonomy', 1 );
function create_format_taxonomy() {
$labels = array(
'name' => _x( 'Formats', 'taxonomy general name', 'mysite' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'resources', 'with_front' => true ),
'show_in_rest' => true,
'rest_base' => 'format',
'rest_controller_class' => 'WP_REST_Terms_Controller',
);
register_taxonomy( 'format', 'resources', $args );
}
add_action( 'init', 'create_resources_cpt' );
function create_resources_cpt() {
$labels = array(
'name' => _x( 'Resources', 'Post type general name', 'mysite' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'resources', 'with_front' => false ),
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'menu_position' => 15,
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions' ),
'taxonomies' => array( 'format', 'section' ),
);
register_post_type( 'resources', $args );
}
function create_section_taxonomy() {
$labels = array(
'name' => _x( 'Sections', 'taxonomy general name', 'mysite' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'resources', 'with_front' => false ),
'show_in_rest' => true,
'rest_base' => 'section',
'rest_controller_class' => 'WP_REST_Terms_Controller',
);
register_taxonomy( 'section', array( 'resources' ), $args );
}
add_action( 'init', 'create_section_taxonomy', 1 );
I am using single_template
and template_redirect
hooks for the parent term static pages, this works fine. However the Format link is not working. If I change the order – register Section before Format, then Format works and Section doesn’t. The only way the single post url works is if I change the slug in the CPT definition to something other than resources.
Leave an answer
You must login or register to add a new answer .