WordPress permalinks are great for having friendly urls that your visitors can also remember and that benefit your SEO optimization. Some solutions will require you to have an url made of a combination of different content. In this tutorial we will look how to have a custom taxonomy and also append the custom post type slug to the end of it.
Reason for a Custom WordPress Rewrite Rule
Let’s look at a scenario to better understand the reason for this. We have a custom post type Resources that also needs a custom taxonomy Type so that we can categorize them under different types of resource. We will display the resources under their category. When our visitors visit an url
www.yoursite.com/resources, they will get a list of resources that are categorized as Featured.
If our visitor visits another url such as
www.yoursite.com/resources/e-books they will get a list of all our resources of type e-books.
So, you do get it right? Our main slug in the url is actually a slug for the taxonomy and not our post type. But, when our visitor clicks on an e-book, they will get to an url such as
my-new-book is a slug for our Resource.
Let’s learn now how to accomplish that.
The Custom Post Type
Our custom post type will be a
resource_post_type. This ensures that the WordPress does not look for the post type when on url
/resources/. We will create our own custom WordPress rewrite rule to handle our resources.
The only part that interests us here is the
taxonomies key that defined a relationship with a taxonomy. Let’s make that taxonomy now.
The Custom Taxonomy
The taxonomy will be registered for our post type Resource. We will also add a rewrite rule immediately when registering it. This will ensure that the WordPress does look for the taxonomy Resource Type when visiting the url
The WordPress rewrite rule is created under the parameter
rewrite. Now we have the url structure set for our taxonomies to be displayed when visiting
resources/ and any sub page such as
The Custom WordPress Rewrite Rule
Now we need to get on the business and create the WordPress rewrite rule to handle third level slugs such as
We are hooking our function
resources_cpt_generating_rule to the filter
generate_rewrite_rules. This filter will be called when flushing rewrite rules. I recommend you go to Settings > Permalinks and just save them once again to flush the rewrite rules.
When our WordPress rewrite rule is being generated, we are getting all our terms from the Resource Type taxonomy. Even those that do not contain any resource relationship. Do take note that you will have to save the permalinks when creating a new taxonomy term, just in case.
Once we get all the terms, we iterate through all of them and create a WordPress rewrite rule for each. The rewrite rule will say something like this:
When you visit
resources/e-books/my-new-book, get the third level slug
my-new-book and set the query string to look for a post type
resources_post_type with the slug (name)
The Custom Post Type Link
So now we have the url structure all set with our new WordPress rewrite rule. But what happens when you save that Resource? The link that is generated once we save our new Resource will be something like
resources_post_type/my-new-book. This is not right.
It should be
resources/e-books/my-new-book. This can be done using the filter
Here we are checking if the post is of type
resources_post_type. This will ensure that we create a different post link only for our Resources. We get all the terms from our custom taxonomy, but only for the current Resource.
While we iterate through all of them, we escape the slug
featured because this is just a secondary type. You don’t have to do that, but if you have a secondary type, don’t include them for the link. Use only the main one.
When we set a term slug, we break out of the loop. We then create a permalink structure that follows our custom WordPress rewrite rule and return it from that filter.
Now you also have a good structured link when saving your Resource.
In this tutorial we have learned how to create custom WordPress rewrite rules and also make a relationship between different content in the url structure. This could benefit the SEO also but more importantly, your visitors will have a really friendly url for the resources they want to see or read.