0

I'm trying to get a rewrite_rules_array rule to work and running into issues. I would like to add a rule so that when someone visits:

http://domain.com/product/foo/bar

It essentially loads:

http://domain.com/product/foo?view=bar

What really happens is that Wordpress redirects you to http://domain.com/product/foo.

add_filter('rewrite_rules_array', function( $rules ) {
    $new = array();
    $new['product/(.*)/(.*)/?'] = 'index.php?pagename=$matches[1]&view=$matches[2]';
    return array_merge( $new, $rules );
});
  • I've flushed my permalinks.
  • view has been added to the query_vars.
2
  • Your products are pages of the page post type? Also- just use add_rewrite_rule hooked to init if you’re just adding rules and not manipulating existing rules or reordering.
    – Milo
    Nov 14, 2018 at 23:00
  • @Milo They are of type 'product'.
    – Louis W
    Nov 15, 2018 at 2:19

1 Answer 1

0

For anyone looking, the solution was:

add_action('init', function() {

     add_rewrite_rule(
        'product/(.*)/(.*)/?',
        'index.php?product=$matches[1]&view=$matches[2]',
        'top'
    );

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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