Skip to main content
1 of 2

Wordpress Rewrite rule for nested post types

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) but it all boils down to how to add rewrite rule(s) to cope with nested post types and still have pagination working.