I have a custom post type of items set up. I also have a taxonomy set up with items called categories.
functions.php code:
regsiter_post_type('items',
array('labels' => array
(
'name' => 'Items',
'singular_name' => 'Item'
),
'public' => true,
'rewrite' => array
(
'slug' => 'items'
),
'supports' => array
(
'title',
'excerpt',
'editor',
'comments',
'revisions',
'thumbnail'
),
'taxonomies' => array
(
'categories',
'tags'
)
));
register_taxonomy ( 'categories', 'items', array('hierarchical' => true,
'label' => 'Item Categories',
'rewrite' => array
(
'slug' => 'categories',
'hierarchical' => ture
)));
require 'rewrites.php'; // load rewrite class
Rewrites::init(); // call init function in rewrite class
rewrites.php code:
class Rewrites
{
// Start class methods
public static function init()
{
// Rewrite urls
add_action ( 'generate_rewrite_rules', array( __CLASS__, 'rules' ) );
add_filter ( 'query_vars', array( __CLASS__, 'vars' ) );
}
public static function rules ( $wp_rewrite )
{
$new_rules = array();
$new_rules['categories/(.+?)/view/(.+)/?'] = 'index.php?categories=$matches[1]&view=$matches[2]';
return $new_rules + $wp_rewrite->rules;
}
// End class methods
}
So when looking at posts in the taxonomy of categories the url would be:
/categories/%name_of_taxonomy%/
I need to also append to the url to add in query_vars
/categories/%name_of_taxonomy%/view/%type_of_view%/
%type_of_view% would be a couple different options. list, grid etc.
Here is my rewrite rule:
[categories/(.+?)/view/(.+)/?] => index.php?categories=$matches[1]&view=$matches[2]
But when I go to the url /categories/%name_of_taxonomy%/view/grid/ it redirects back to /categories/%name_of_taxonomy%/
Found the solution. The rewrites were getting added to the db after the system was reading them from the db. Moved the code and it worked!