Enhanced custom post types URL rewriting

With GD Custom Posts And Taxonomies Tools 3.2.2 Pro, rewriting rules for custom post types single posts and archives are enhanced even further, and this tutorial will describe how you can set different things for the permalinks rules and structures and make better use of it.

Single Post Permalinks

To see this in action, check out the video tutorial also: Custom permalinks for custom post types. Much is said before about links for the custom post types. But, here is the short version: when you create custom post type, WordPress will allow only one type of URL for posts belonging to custom post type, and that is a combination of post type slug and slug for the post. You can’t get fancy URL like you can set for default posts. But, with this plugin you can. To set rules for custom post type created with the plugin, open post type in edit mode, Rewrite tab you will see this:

Single Post Rewrite Rules
Single Post Rewrite Rules

You can enable it or disable it and set permalinks structure. Empty spaces are not allowed. Also, you see the list of allowed elements for the URL.You can use any of them, and mix them with normal words. On the image above, first part of permalink ‘document’ is a word that will appear in each permalink. Tags with % symbols will be replaced with actual values for the post URL is generated. While the permalink structure set here is active, all WordPress functions will use this new link, and get_permalink() function will use it, you don’t need to change your code for this.

To make a proper permalink, you must use one of the two (or both) post type elements. They are different for each post type. As for taxonomies, use only taxonomies that are attached to post type. If they are not attached or they don’t have the term for the post URL will have taxonomy tag replaced by dash, and not actual term slug.

So, for the example on the image, as you can see, we use word document, taxonomy context and post type name. Let’s see some more complex structures and possible links for it:



As you can see, it is easy to set up. All rules will be regenerated as soon as you save settings.

Date based archives

On same rewrite tab, you will see more options for setting up the custom post type archives:

Archives Rewrite Rules
Archives Rewrite Rules

To enable date based archives, simply enable first option in this group. Archives URL’s will be based on the normal custom post type archives, and it will look like this:


Simple archive intersections

Second rewrite box is for archives intersections. There are two methods to use, and you can have both of them active. First one, simple has no other settings, it will always use same structure. First, by default custom post types archive link is again very simple and limited, and only has custom post type archive slug, and you will get all posts for the custom post type. If you need something more complex, to filter posts by terms, you can’t do it with clean URL. When you enable simple intersections, you can get archive links that will show posts for custom post types, filtered with term in the permalink. Strucutre for simple intersections is this:


For same example as before, if we have post type with archive slug set to documents, here are two examples:


First one will display all posts belonging to documents, that have term gd-press-tools for product taxonomy. Second one will filter by context taxonomy, function term. Easy, very easy.

Advanced archive intersections

This one is a bit more complex, and involves mixing more than one taxonomy for the custom post type archive filtering. This one uses permalinks structure, as on the image here:

Permalinks here is not really permalink, is the structure and order of taxonomies to use. So if you have custom post type with 2 or more taxonomies, this is one to use. If you have custom post type with one taxonomy, don’t use advanced intersection. Don’t use taxonomies in structure that are not adding to the custom post type. Permalink set here will be added to base custom post type archive link. Important thing is order of the terms here. Structure shown on the image above will include two terms in the link. But, unlike the simple intersections, names of the taxonomies are not part of the URL, only term slugs.


If you have post type ‘movie’ with these 3 taxonomies: category, genre, post_tag, you can set rewrite rule for advanced intersection as:


Advanced intersection link will need all 3 to resolve, in the order you specified for the rule. Link could like this:


What will happen to hierarchical taxonomies?

This will still work, but not exactly as you might expect it. Rewriting rules are very complex and for the rule to work it must be resolvable in the first place, if the rule is ambiguous you will have a problem. So, if the taxonomy is hierarchical, term slug will be in sequence containing parent term slugs. But, for these interactions we can’t have parent slugs included, only real slug for the term.

Functions to generate links

For single post permalinks, use as before get_permalinks() function. As for the intersections, there are two functions for this, and they are in the documentation:


Limitations and recommendations

Limitations are already listed on the edit pages. But, the most important limitation is WordPress. If you have many custom post types, and you try setting permalinks that clash with other rules, that will make a problem for WordPress to resolve links. If you create structures that have similar number of elements as some of the existing rewrite rules that WordPress can consider ambiguous, it will cause for the links to resolve to wrong destination. It is best to test everything to make sure that there are no problems.

For single post permalinks, my only recommendation is to use a word to start URL (as in the examples above). This is almost 100% guarantee that URL will work as expected. Don’t use same slug for single post and archives. Even if you start URL with tag element, it will work if there are no conflicts with other rewrite rules.

Archive intersections can be tricky to resolve in some cases. For simple intersections, as long as the post type slug for archives is unique, you will have no problems. But, for advanced structure, URL with 2 or 3 taxonomies can class with some other rules. Plugin can’t help you much, you need to test the rules.

On Dev4Press I have 6 custom post types, and various combinations of archive and single rewrite structures, and I have no problems. But, just in case, you must know that conflicts can happen, test to avoid them. To see both simple and advanced intersection in action, check out our Central Documentation (you must log in to be able to access it):



Leave a Reply