Tag Archives: taxonomy

Prefixing a Custom Taxonomy in WordPress

I’m currently working on a website with a custom portfolio section. It’s making use of a custom post type called project and two attached custom taxonomies called market, and service. The portfolio page exists here:

example.com/portfolio/

displaying all the projects. Each project live here:

example.com/projects/<project-slug>

Everything was going fine until I realized that my custom taxonomies were not as separated from the main blog as I would like them to be. Consider the following taxonomy archive URLs:

example.com/market/<market-slug>
example.com/service/<service-slug>

and the very similar pages already on the site:

example.com/services/
example.com/services/service1
example.com/services/service2

etc. What I needed to do was to group the taxonomy archives under the portfolio page in the URL structure, turning

example.com/market/<market-slug>

into

example.com/portfolio/market/<market-slug>

But how does one prefix a custom taxonomy URL in WordPress with a static value such as ‘/portfolio/‘ ? The answer is pretty easy, it turns out.

Put That Slash in the Slug

All you need to do is add the static prefix value to the slug portion of the rewrite argument when registering the taxonomy. For example:

$serviceArgs = array(
		'labels'     => $serviceLabels,
		'query_var'  => 'service',
		'rewrite'    => array( 'slug' => 'portfolio/service' ) //was 'service'
	);
	register_taxonomy( 'project_service', null, $serviceArgs );

After you make the update, flush your rewrite rules and you are good to go!

More info on register_taxonomy().