-3

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?

toby845
  • 3
  • 2
  • Welcome to StackOverflow. To prevent downvotes or closure, please review [**how do I ask a good question**](https://stackoverflow.com/help/how-to-ask) and edit your question to meet this [**Question Checklist**](https://meta.stackoverflow.com/questions/260648/). Questions are likely to get downvoted when they show no evidence of *research* or *a good attempt to solve the problem yourself*, or do not include: *specific details* of the error, *what you have tried so far*, and your *relevant code* in a [minimal,reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – FluffyKitten Jul 17 '20 at 15:02

1 Answers1

1

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();
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sco
  • 749
  • 1
  • 7
  • 13
  • Hi @Dharman, thanks for your help. However I get the results I want from this article https://wisdmlabs.com/blog/add-taxonomy-term-custom-post-permalinks-wordpress/ – toby845 Jul 17 '20 at 22:09