Abstract
I've been tasked to figure out how to use the Wordpress Rewrite API to create a URL structure that can filter posts of a products
custom post type. When passing terms of taxonomies through a URL structure Wordpress would natively filter the products
posts and return the results to the archive-products.php
template.
Example data set of taxonomies
and their terms
of products
Solutions
Series
Industries
Tech
Examples of desired URLs
products/solutions/broadband,enterprise/series/express/industries/business,education
products/industries/tech/internet/solutions/broadband,express
products/solutions/broadband
The above is just an idea for a structure any others are more than welcome.
Problem
Due to my lack of regex skills I'm not sure what is possible or not. Any point in the right direction would be greatly appreciated.
Go through the below code. It's may help you.
function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
$rules = array();
$post_types = get_post_types( array( 'name' => 'products', 'public' => true, '_builtin' => false ), 'objects' );
$taxonomies = get_taxonomies( array( 'name' => 'Solutions', 'public' => true, '_builtin' => false ), 'objects' );
foreach ( $post_types as $post_type ) {
$post_type_name = $post_type->name; // 'product'
$post_type_slug = $post_type->rewrite['slug']; // 'products'
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');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.