so i’m back with rewrites… i am looking for a more automated solution.
i stumbled across this on the interwebs:
/**
* Generates all the rewrite rules for a given post type.
*
* The rewrite rules allow a post type to be filtered by all possible combinations & permutations
* of taxonomies that apply to the specified post type and additional query_vars specified with
* the $query_vars parameter.
*
* Must be called from a function hooked to the 'generate_rewrite_rules' action so that the global
* $wp_rewrite->preg_index function returns the correct value.
*
* @param string|object $post_type The post type for which you wish to create the rewrite rules
* @param array $query_vars optional Non-taxonomy query vars you wish to create rewrite rules for. Rules will be created to capture any single string for the query_var, that is, a rule of the form '/query_var/(.+)/'
*
* @author Brent Shepherd <me@brentshepherd.com>
* @since 1.0
*/
function eg_generate_rewrite_rules( $post_type, $query_vars = array() ) {
global $wp_rewrite;
if( ! is_object( $post_type ) )
$post_type = get_post_type_object( $post_type );
$new_rewrite_rules = array();
$taxonomies = get_object_taxonomies( $post_type->name, 'objects' );
// Add taxonomy filters to the query vars array
foreach( $taxonomies as $taxonomy ){
//$query_vars[$taxonomy->rewrite['slug']] = $taxonomy->query_var;
$query_vars[] = $taxonomy->rewrite['slug'] ;
add_rewrite_tag('%'.$taxonomy->rewrite['slug'].'%','([^&]+)');
}
// Loop over all the possible combinations of the query vars
for( $i = 1; $i <= count( $query_vars ); $i++ ) {
$new_rewrite_rule = $post_type->rewrite['slug'] . '/';
$new_query_string = 'index.php?post_type=' . $post_type->name;
// Prepend the rewrites & queries
for( $n = 1; $n <= $i; $n++ ) {
$new_rewrite_rule .= '(' . implode( '|', $query_vars ) . ')/(.+?)/';
$new_query_string .= '&' . $wp_rewrite->preg_index( $n * 2 - 1 ) . '=' . $wp_rewrite->preg_index( $n * 2 );
}
// Allow paging of filtered post type - WordPress expects 'page' in the URL but uses 'paged' in the query string so paging doesn't fit into our regex
$new_paged_rewrite_rule = $new_rewrite_rule . 'page/([0-9]{1,})/';
$new_paged_query_string = $new_query_string . '&paged=' . $wp_rewrite->preg_index( $i * 2 + 1 );
// Make the trailing backslash optional
$new_paged_rewrite_rule = $new_paged_rewrite_rule . '?$';
$new_rewrite_rule = $new_rewrite_rule . '?$';
// Add the new rewrites
$new_rewrite_rules = array( $new_paged_rewrite_rule => $new_paged_query_string,
$new_rewrite_rule => $new_query_string )
+ $new_rewrite_rules;
}
return $new_rewrite_rules;
}
// add the rules
function mv_add_rewrite_rule($wp_rewrite){
$new_rules = eg_generate_rewrite_rules('product');
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules','mv_add_rewrite_rule',99);
http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/
but while this works great if you don’t have rewrite slugs set for each custom taxonomy, it fails if your rewrite slug is something different from your query var. (since it is auto-adding the query_var grabbed in the $match as the query string.
how can i amend this function so that it will match the rewrite[‘slug’] but add the associated query_var to the query string.
phew… i hope that made sense. OR, does this sound possible at all? would it be advisable to hard-code the re-write rules instead?
product-categories/fabrics/color/red
product-tags/new/style/jacquard
( product-category OR product-tag ) / ( color OR style OR material trim-style)
As I find myself googling about rewrites again, a full year later I will post the solution that was provided to me by the tutorial’s author. The solution was to set the desired
query_var
when registering the taxonomy.