Reputation: 21924
I finally got my permalinks working for Custom Post Types and Taxonomies using `a helper class.
My custom post type is "menu" and my taxonomy is "menutype".
I wrote the following rewrite:
function custom_rewrite( $wp_rewrite ) {
$feed_rules = array(
'menu/salads/(.+)' => 'index.php?menu=' . $wp_rewrite->preg_index(1),
'menu/hotboxes/(.+)' => 'index.php?menu=' . $wp_rewrite->preg_index(1),
'menu/wraps/(.+)' => 'index.php?menu=' . $wp_rewrite->preg_index(1),
'menu/sauces/(.+)' => 'index.php?menu=' . $wp_rewrite->preg_index(1),
'menu/soups/(.+)' => 'index.php?menu=' . $wp_rewrite->preg_index(1),
'(.+)/([^/]+)(/[0-9]+)?/?$' => 'index.php?menutype=' . $wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
// refresh/flush permalinks in the dashboard if this is changed in any way
add_filter( 'generate_rewrite_rules', 'custom_rewrite' );
The only thing is I would like to not hardcode the taxonomy (i.e menu/sauces/, menu/hotboxes) into my rewrite rules as the terms might change (but they will always be lowercase letters)
I tried the following:
'menu/([^/]+)/(.+)'
And also:
'menu/([a-z]+)/(.+)'
But neither worked.
Upvotes: 0
Views: 1264
Reputation: 19539
If you don't need the reference to what follows menu/
, then remove the parens there, e.g.
menu/[^/]+/(.+)
If you do need the reference, then make sure you're passing the correct int argument to preg_index.
As to "why does removing the parens work", because including the parens around the first group [^/]+
creates a back-reference to what's matched there. So when you call preg_index(1)
you're asking for the first match, when what you want is actually the 2nd match .+
. You could also pass 2
to preg_index
indicating that you want the 2nd matched group...but if you don't need the first match it's just wasted overhead to create the reference in the first place.
Upvotes: 1