1

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:

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

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

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

/moving-to/

/moving-to/continent

/moving-to/continent/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

/moving-to/continent/category-name

/moving-to/continent/country/category-name

Again with pagination

/moving-to/continent/category-name/page/2

/moving-to/continent/country/category-name/page/2

/moving-to/continent/country

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

/moving-to/
/moving-to/continent/
/moving-to/continent/country
/moving-to/page/2
/moving-to/continent/page/2
/moving-to/continent/country/page/2
/moving-to/continent/category
/moving-to/continent/country/category
/moving-to/continent/category/page/2
/moving-to/continent/country/category/page/2
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, privacy policy and cookie policy

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