Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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%/

share|improve this question
    
Ayers - How are you adding that rewrite rule? Can you update your question with the code? –  MikeSchinkel Mar 11 '11 at 12:07
    
@MikeSchinkel I have added it –  cointilt Mar 11 '11 at 19:38
    
If I change 'rewrite_rules_array' to 'generate_rewrite_rules' and then use the wp_rewrite object instead of $rules array it won't redirect and leaves the url correct, but shows a 404 page not found wordpress page. –  cointilt Mar 11 '11 at 21:10
    
I was looking for you to provide complete code that someone can test vs. a segment of your code where if we wanted to test it we have to guess at what else you wrote. What you added was a static class method but you didn't add the rest of the class so we can't be sure your error isn't somewhere else. –  MikeSchinkel Mar 13 '11 at 6:58
    
Ok I changed the code to show all of what I am using now in my functions.php file and in the class file for rewrites. –  cointilt Mar 14 '11 at 16:40
add comment

1 Answer

up vote 0 down vote accepted

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!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.