Enhanced custom post types URL rewriting
GD Content Tools Pro introduces options for setting up custom rewrite rules and permalinks for custom post types. There are 3 types of enhanced URL’s implemented by the plugin.
Single Post Permalinks
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, and it can go even further then the WordPress for posts can do.
Image shows single permalinks panel. You have the option to enable the rules. There is a list of permalinks examples you can use to get started. And, below the field for the rule, you have elements you can use in the URL. These elements will be replaced by actual values in the URL. And, you can use words (with dashes) or slugs. Example image shows the rule for the post type ‘movie’:
movie/%year%/%monthnum%/%movie%/
Permalink starts with the custom word/slug ‘movie’. Followed by the year and month values. Finally, the last element is the unique tag for this post type, and it is replaced by the specific post slug.
Examples
Here are few examples. Each example contains the permalink structure and example URL generated using that rule.
movie/%year%/%monthnum%/%movie%/ http://www.example.com/movie/2016/10/some-movie-name/
movie/%genre%/%year%/%movie%/ http://www.example.com/movie/western/2015/some-movie-name/
Custom post types date based archives
Once you enable date based post types archives, you will get dates – years, months and day URL’s related to the custom post type. So, you will get URL’s like:
http://www.example.com/movies/2015/ http://www.example.com/movies/2016/10/
URL uses custom post type archive slug and date-based structure. This will display posts belonging to custom post types, filtered by date.
Custom post type archives intersections
One of the major complaints about the URL structure in WordPress is that you can’t specify terms inside the URL link to filter the posts in the post type archive.
Normal WordPress custom post type archive URL is:
http://www.example.com/movies/
To filter by term URL would be:
http://www.example.com/movies/?genre=western
Simple Intersections
If you don’t like to use query string for this, you can use this plugin to have URL like this one:
http://www.example.com/movies/genre/western/
Or even this:
http://www.example.com/movies/western/
The first case includes specification of taxonomy and term slug. The second example is where the taxonomy is omitted. If post type has more then one taxonomy, only one can be set to work without the taxonomy specified in the URL. This is the option called Baseless taxonomy, and one taxonomy can be specified like baseless for every custom post type.
Specifying one taxonomy and one term in that taxonomy is called ‘simple intersection’.
Advanced Intersections
If you have post type with two or more taxonomies, you can build archive URL that contains terms for each taxonomy. So, if you have 3 taxonomies: genre, category, and tag, you can create advanced permalinks structure:
%genre%/%category%/%post_tag%/
So, the URL will end up look like this:
http://www.example.com/movies/western/my-cat/my-tag/
This will filter movies that have genre ‘western’, category ‘my-cat’ and tag ‘my-tag’. It is important to note that first taxonomy in the structure must be the same one you use for the baseless taxonomy option. And if you enable partial intersection, you can specify only first two terms, omitting the third taxonomy.
Limitations for custom rules
There are few limitations with custom permalinks. All limitations are due to the way WordPress rewrite engine works.
- 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 a 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 the wrong destination.
- For simple intersections, as long as the post type slug for archives is unique, you will have no problems.
- For advanced structure, URL with 2 or 3 taxonomies can clash with some other rules.
- Empty spaces are not allowed anywhere in the permalink structure.
- Once you change the permalink structure, that structure will be used from that point on. Old URL’s generated by previous structure will stop working!
- When you make any change to the rules, make sure to visit Permalinks panel in the WordPress settings, so that permalinks can be rebuilt.
You must test everything to make sure that there are no problems.
Recommendations
To create structures that will work, make sure to follow some recommendations.
- For single post permalinks, use a unique word to start URL (as in the examples above). This should guarantee that URL will work as expected.
- Try not to use the same slug for a single post and archives.