Site icon Hip-Hop Website Design and Development

add_rewrite_rule with elective parameters

Utilizing add_rewrite_rule() to deal with issues like this for the customized put up sort "catalogs", web page identify "cars":

/catalogs/automobiles/
/catalogs/automobiles/p1/
/catalogs/automobiles/p1/p2/
/catalogs/automobiles/p1/p2/p3/

I by no means have greater than 3 parameters after /automobiles/, however all of them with elective question string parameters. Like this:

/catalogs/automobiles/p1/p2/?weight-max=3200

Here is my code:

add_rewrite_rule( 
    '^catalogs/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?', 
    'index.php?catalogs=$matches[1]&p1=$matches[2]&p2=$matches[3]&p3=$matches[4]', 
    'prime'
);

Works nice for when there’s 3 parameters (like /catalogs/automobiles/p1/p2/p3/), but it surely does not work for when there’s 0-2 parameters.

Have tried making elements of the regex elective, however I assume my regex abilities aren’t that implausible…

Have a trick for making the variety of parameters dynamic, so every little thing from 0-3 parameters may be dealt with?

Thanks :thumbsup: