WordPress Rewrite rule for nested post types

The main problem with the regex based way wordpress does URL parsing/routing/”rewriting” is that regexp lacks any context. All the URLs of countries you will have need to be in the specific context of a continent. If there are two countries with the same name best country, one in continent big and the other in continent small, you will want a url like /big/best-country and /small/best-country.

WordPress needs/tries to understand which page to load directly based on the URL without any additional DB access, by locating its slug in the URL, but it just can’t do it in this structure. It can find the slug of the continent, but the slug of the country is problematic as there are two possible results. WordPress will actually never let you get into such situation unless you force it into, and will attach a suffix -nnn when a second post is created with a slug that already exists.

The only way around it is to implement your own parsing before wordpress’s kicks in.

But…. maybe you are making your life complex for no reason at all, since luckily on earth there are no two countries with the same name, so what you might be able to do is to use slugs like continent-country and use .htaccess rule to convert continent/country into continent-country. For this to fully work correctly your will also need to mess with the way permalinks are computed but it is a much easier problem to solve.

Leave a Comment