Search code examples
phpwordpresscustom-post-typecustom-taxonomy

How to Add Multiple Taxonomies in Custom Post Permalink in WordPress URL structure


I have created a custom post type called Business Listing with multiple taxonomies("Specialty, State, City") For example: Custom post name: Dr. Jones Dental Taxonomy (Specialty): General Dentist Taxonomy (State): California Taxonomy (City): Pasadena

So currently, the url looks like this: https://test.com/business-listing/dr-jones-dental

And this is my desired URL structure should look: https://test.com/business-listing/general-dentist/california/pasadena/dr-jones-dental

However, I need to to get a perfect URL structure that is SEO friendly. So, the final URL structure should be like this: https://test.com/general-dentist/california/pasadena/. So how can I achieve this one without using a 301 redirection?


Solution

  • I give you my code what I use in your case


    class BusinesListing
    {
    
    
        public function __construct()
        {
            // CPT
            add_action('init', [
                $this,
                'create_busines_listing_cpt'
            ], 0);
    
            // TAXOS
            add_action('init', [
                $this,
                'create_taxo_speciality'
            ]);
            add_action('init', [
                $this,
                'create_taxo_state'
            ]);
            add_action('init', [
                $this,
                'create_taxo_city'
            ]);
    
            // URLs
            add_filter('post_type_link', [
                $this,
                'custom_permalink'
            ], 1, 2);
        }
    
        public function create_busines_listing_cpt()
        {
            register_post_type('busines_listing', [
                // ...
                'taxonomies' => array(
                    'taxo_speciality',
                    'taxo_state',
                    'taxo_city'
                ),
                'public' => true,
                'rewrite' => array(
                    'slug' => 'busines-listing/%taxo_speciality%/%taxo_state%/%taxo_city%',
                    'with_front' => true
                ),
                // ...
            ]);
        }
        public function create_taxo_speciality()
        {
            register_taxonomy('taxo_speciality', ['busines_listing'], [
                // ...
                'label' => 'speciality',
                'rewrite'=>  array(
                    'slug' => 'busines-listing',
                    'with_front' => true
                ),
                // ...
                ]
            );
        }
        public function create_taxo_state()
        {
            register_taxonomy('taxo_state', ['busines_listing'], [
                // ...
                'label' => 'state',
                'rewrite'=>  array(
                    'slug' => 'busines-listing/%taxo_speciality%',
                    'with_front' => true
                ),
                // ...
                ]
            );
        }
        public function create_taxo_city()
        {
            register_taxonomy('taxo_city', ['busines_listing'], [
                // ...
                'label' => 'city',
                'rewrite'=>  array(
                    'slug' => 'busines-listing/%taxo_speciality%/%taxo_state%',
                    'with_front' => true
                ),
                // ...
                ]
            );
        }
    
        public function custom_permalink($post_link, $post)
        {
            // url
            if (is_object($post) && $post->post_type == 'busines_listing') {
                $terms = wp_get_object_terms($post->ID, 'taxo_speciality');
                if (! empty($terms)) {
                    $post_link =  str_replace('%taxo_speciality%', $terms[0]->slug, $post_link);
                }
                $terms = wp_get_object_terms($post->ID, 'taxo_state');
                if (! empty($terms)) {
                    $post_link =  str_replace('%taxo_state%', $terms[0]->slug, $post_link);
                }
                $terms = wp_get_object_terms($post->ID, 'taxo_city');
                if (! empty($terms)) {
                    $post_link =  str_replace('%taxo_city%', $terms[0]->slug, $post_link);
                }
            }
            return $post_link;
        }
    }
    new BusinesListing();