Posted by on Apr 7, 2016 in Development, PHP | No Comments

I recently was working on a project where I had two custom Post Types with a parent/child relationship between them. Here’s an example:

  • Custom Post Type “Brand” where values such as “Apple” or “Samsung” could be saved.
  • Custom Post Type “Product” where values such as “iPhone or “Galaxy” could be saved.

I used the plugin Types Toolset to create the custom Post Types and add their relationship but this could be done with other Post Type creator plugin or setup manually with PHP, the plugin just worked well for my needs.

I knew that I wanted a Permalink base for this section of the website to be my-site.com/devices/ which was easy enough to create using the plugin. Now within this section I wanted to setup a Permalink structure of /devices/brand/product so that I could have a really readable URL like my-site.com/devices/apple/iphone/ (pretty, isn’t it?) but the Toolset plugin (as well as any other plugin I know of) only works with static Permalink bases for custom Post types. Creating this dynamically would take a little work.

My solution (see below) is two parts, first when the custom Post Type is saved rewrite the Permalink to what I want, secondly tell WordPress how to handle requests to that Permalink by adding a rewrite rule since it’s unaware of that Permalink structure otherwise.

Tagged: ,