When we create Custom Post Type in WordPress website, we usually have to add Taxonomies in the CPT and then have to make taxonomy part of url for SEO demands.
There are many ways to create CPT, I won’t go into the details, you can simply use ACF or Custom Post Type UI Plugins to create CPT and Taxonomies in your WordPress website, i will use CPT UI plugin example but the process is same for all.
Create the CPT using CPT UI plugin, go to the Edit CPT, scroll down to the “Custom Rewrite Slug” option field and add your taxonomy, let’s say, your CPT is Attractions and slug is: attractions, you should define custom rewrite slug to attractions/%taxonomy% , see the screenshot below>>
This is the first step.
In the second step, add the following php code in your theme functions.php file or code snippet:
function pim_attractions_custom_taxonomy_link($link, $id = 0){ $post = get_post($id); if (is_object($post)) { $terms = wp_get_object_terms($post->ID, 'attraction'); if ($terms) return str_replace( '%taxonomy%', $terms[0]->slug, $link ); } return $link; } add_filter('post_type_link', 'pim_attractions_custom_taxonomy_link', 1, 3); function pim_attraction_taxonomy_rewrite() { add_rewrite_rule( '^attractions/(.*)/(.*)/?$', 'index.php?post_type=attractions&name=$matches[2]', 'top' ); } add_action('init', 'pim_attraction_taxonomy_rewrite');
First function will replace the %taxonomy% with the actual added value say, “places-to-visit”. My taxonomy name is attraction. you have to replace to your taxonomy.
Second function will simply rewrite it. Note that my CPT is attractions and it is part of rewrite rule. You have to replace it with your CPT.
After adding this code, don’t forget to flush the permalinks by going to Settings > permalinks and save once.
Hope that helps.