1

I am working on a site for cars. I wrote a regex which worked, up until the point where brands are separated by a hyphen.

These are 2 examples I want to match:

  • class-s/ford-shelby-gt350r
  • class-s/exotic-rides-w70

The regex I used for it is

([a-z-]+)\/([a-z]+[-]*[a-z]+)-([a-zA-Z0-9]+[-]*[a-z0-9]+)\/?$

But this matches Shelby as the brand and not as type.

This is how I use it inside Wordpress

$wp_rewrite->add_permastruct( 'cars', 'cars/%class%/%brand%-%postname%/', false );
add_rewrite_rule( 'cars/([a-zA-Z-]+)/([a-zA-Z]+)-([a-zA-Z0-9-]+)/?$', 'index.php?post_type=as_car&name=$matches[3]', 'top' );

I think I need to check (with a lookup) if the brand contains a hyphen, but I don't know how to do it.

Can someone help me do it ?

This is a link to my regex editor: https://regex101.com/r/sI7bW4/138

ADDED: I changed my code. I now generate a full post_name, so I don't have to regex anything...

3
  • you can try to save "ford-shelby-gt350r" in a meta value of the car and then you search for the complete value to find which car to show.
    – Kaperto
    Jan 4 '20 at 21:25
  • it is not a meta value, it's a combination of taxonomy (brand) + post title (type) and I prefer not to store stuff which can be generated.
    – Beee
    Jan 5 '20 at 1:03
  • Also your answer is not what I asked, I asked how to do a loopup, not try to fix it another way.
    – Beee
    Jan 5 '20 at 1:17

Your Answer

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

Browse other questions tagged or ask your own question.