0

Here's the setup.

I have a custom post type 'Businesses'. I have a custom taxonomy 'Display Category'. And I have a second custom taxonomy 'Filters'

Both taxonomies are set to work with the custom post type.

I am trying to get a url structure that looks like this

www.domain.com/businesses/display category term/filter term

There are only 3 possible choices for the Display Category, those are eat,stay, and play. The Filters then have quite a few choices. so here is the rewrite rule I have so far but it's not working and I'm not sure why.

function test_add_rewrite_rules() {

    global $wp_rewrite;

    $new_rules = array(
        'businesses/(eat|stay|play)/(.+)/?$' => 'index.php?post_type=businesses&display_category=' . $wp_rewrite->preg_index(1) . '&filters=' . $wp_rewrite->preg_index(2),
        'businesses/(eat|stay|play)/?$' => 'index.php?post_type=businesses&display_category=' . $wp_rewrite->preg_index(1)

    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

add_action( 'generate_rewrite_rules', 'test_add_rewrite_rules' );

I am using a rewrite rule inspector plugin so I know the rules are being added. But they are not returning the pages I want. I found this article about rewrite rules and it helped me get this far but I am stuck now. Any help would be greatly apprecieated!

Update

Here is the above code but instead using the add_rewrite_rule function. I have hooked in into init for now because the generate_rewrite_rules hook was firing too late for the new rules to be save. I also realized that I will need to get the paged rules to work as well but I'm not sure how to do that.

function test_add_rewrite_rules() {

    global $wp_rewrite;

    add_rewrite_rule('businesses/(eat|stay|play)/(.+)/?$', 'index.php?post_type=businesses&display_category=$matches[1]&filters=$matches[2]', 'top');
    add_rewrite_rule('businesses/(eat|stay|play)/?$', 'index.php?post_type=businesses&display_category=$matches[1]', 'top');

}

add_action( 'init', 'test_add_rewrite_rules' );
3
  • Only a technical note, it's preferable to use add_rewrite_rule(). Sep 9, 2014 at 20:10
  • Ok, thanks for the heads up. I will switch over to using that function. Any idea what hook I should use to run it?
    – yoxalld
    Sep 10, 2014 at 15:09
  • init should be sufficient Sep 10, 2014 at 15:14

1 Answer 1

0

You have to make your rewrite rules account for the paged parameter, because otherwise they won't. It would look somewhat like shown below.

function test_add_rewrite_rules() {
    add_rewrite_rule(
        'businesses/(eat|stay|play)/(.+)/page/([0-9]+)/?$',
        'index.php?post_type=businesses&display_category=$matches[1]&filters=$matches[2]&paged=$matches[3]',
        'top'
    );
    add_rewrite_rule(
        'businesses/(eat|stay|play)/page/([0-9]+)/?$',
        'index.php?post_type=businesses&display_category=$matches[1]&paged=$matches[2]',
        'top'
    );
}
add_action( 'init', 'test_add_rewrite_rules' );
2
  • Thanks for the help! I also figured out that I have to order the rules in a specific way. I had to put the paged rules above the regular rules for them to tested first. Which is a bit counter intuitive when you also use the 'top' argument with add_rewrite_rules(). But I got it working in the end!
    – yoxalld
    Sep 10, 2014 at 15:59
  • My pleasure! True, its a bit counter intuitive, but there is no priority or something else, so you have to order them somehow. Besides top only does mean they are checked before WordPress' rules. @yoxalld Sep 10, 2014 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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