I have a fairly complicated site structure and cannot seem to get the URL structure to work correctly, including pagination. This is mainly to cope with loading articles assigned to a specific custom post type, which may all be nested.

My (cut down) register post types:

        'rewrite' => array('slug' => 'moving-to'),

        'rewrite' => array('slug' => 'moving-to/%continent%'),

So I have a category moving-to, a custom post type continent and a custom post type country.




These first three display posts from the category, posts assigned to the continent and posts assigned to the country. I cannot get pagination to work with them as I think the /continent/country is interfering with the /page/n pagination.

/moving-to/page/2 - Works OK

/moving-to/continent/page/2 - Tries to load unknown country "page"

/moving-to/continent/country/page/2 - Strips page/2 from the URL

I assume I need to exclude any country of "page" from the rewrite, or to just not have a nested continent in the url.

Part two is being able to have a category after each continent or country



Again with pagination




I realise this all sounds a bit specific (borderline off-topic) I'm basically asking how to enable the following URL structures:


1 Answer 1


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.

Your Answer

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

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