# Desired permalink structure using region and category

**URL:** <https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929>\
**Category:** HivePress\
**Created:** [February 8, 2025, 3:57am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929 "2025-02-08T03:57:08Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![kja](https://community.hivepress.io/letter_avatar_proxy/v4/letter/k/a87d85/32.png) [@kja](https://community.hivepress.io/u/kja)\
**Post date:** [February 8, 2025, 3:57am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/1 "2025-02-08T03:57:08Z")

</div>

Hello,

I enabled generate regions from location in Geolocation settings. It’s creating the region successfully where a new listing creates the State \> County \> City hierarchy in regions.

I’m trying to figure out how to structure my permalinks using regions and categories to look like this:

[mysite.com/state/city/category/listing-slug](http://mysite.com/state/city/category/listing-slug)

Is there a way to do this? I don’t want the county…just state and city.

---

<div class="post-metadata">

**Author:** ![andrii](https://community.hivepress.io/user_avatar/community.hivepress.io/andrii/32/2951_2.png) [@andrii](https://community.hivepress.io/u/andrii)\
**Post date:** [February 10, 2025, 10:25am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/3 "2025-02-10T10:25:08Z")

</div>

Hi,

You can change the permalink structure using this documentation: [How to customize URL structure - HivePress Help Center](https://help.hivepress.io/article/158-how-to-customize-urls). All other settings will require custom implementation, .e.g [Using Post ID in Listing URL](https://community.hivepress.io/t/using-post-id-in-listing-url/9661)

​I hope this is helpful to you

---

<div class="post-metadata">

**Author:** ![kja](https://community.hivepress.io/letter_avatar_proxy/v4/letter/k/a87d85/32.png) [@kja](https://community.hivepress.io/u/kja)\
**Post date:** [February 10, 2025, 5:47pm UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/4 "2025-02-10T17:47:52Z")

</div>

Thanks, Andrii.

Sorry I wasn’t more clear. I did follow the how to and currently have [mysite.com/state/listing-slug](http://mysite.com/state/listing-slug) instead of the default [mysite.com/listing/listing-slug](http://mysite.com/listing/listing-slug), however, what I am trying to figure out is how to pull in the region and category values.

The %category% structure tag doesn’t pull in the listing category. For example, I tried to use /state/%category%/%postname%/ as a custom structure and that does not work.

I can change listing to state, but how do I pull in the city from the listing’s region and category from the listing’s category?

---

<div class="post-metadata">

**Author:** ![andrii](https://community.hivepress.io/user_avatar/community.hivepress.io/andrii/32/2951_2.png) [@andrii](https://community.hivepress.io/u/andrii)\
**Post date:** [February 12, 2025, 9:36am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/8 "2025-02-12T09:36:49Z")

</div>

Hi,

Such settings are beyond the available options and can only be added with the help of custom code, as WordPress does not have such permalink settings for custom taxonomy. You can find the example at this link: [php - How to add custom taxonomy in custom post type permalink? - Stack Overflow](https://stackoverflow.com/questions/57765487/how-to-add-custom-taxonomy-in-custom-post-type-permalink), listing is `hp_listing` post type, regions is `hp_listing_region` taxonomy.

I hope it helps

---

<div class="post-metadata">

**Author:** ![kja](https://community.hivepress.io/letter_avatar_proxy/v4/letter/k/a87d85/32.png) [@kja](https://community.hivepress.io/u/kja)\
**Post date:** [February 15, 2025, 3:52am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/9 "2025-02-15T03:52:58Z")

</div>

Thanks for the example, andrii. I played around with it and created a snippet. Although I was able to get the custom permalink structure I wanted when adding a new listing, unfortunately, the link goes to a nothing found page.

I was hoping to include the city and category to help with local SEO, but not really sure if it makes a difference or not. I just notice sites like Tripadvisor and YP do this, but Yelp does not.

If anyone else has an interest in something like this let me know and I’ll post the snippet. Otherwise, I’ll probably settle for changing the base from listing to my state and call it good. I don’t want this to hold up my launch, but I also don’t want to build out a bunch of links and try to change them later if this is possible.

---

<div class="post-metadata">

**Author:** ![condorito.fr](https://community.hivepress.io/user_avatar/community.hivepress.io/condorito.fr/32/11449_2.png) [@condorito.fr](https://community.hivepress.io/u/condorito.fr)\
**Post date:** [February 15, 2025, 6:26am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/10 "2025-02-15T06:26:14Z")

</div>

Hello @kja

Did you save your permalinks (under Settings) ?

Yes, share your snippet, by all means

---

<div class="post-metadata">

**Author:** ![kja](https://community.hivepress.io/letter_avatar_proxy/v4/letter/k/a87d85/32.png) [@kja](https://community.hivepress.io/u/kja)\
**Post date:** [February 15, 2025, 8:04pm UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/11 "2025-02-15T20:04:09Z")

</div>

Thanks @condorito.fr

Yes, I save the permalinks to flush the rewrite rules.

OK, I think I got it working! (with the help of Claude) Please forgive my noobiness.

NOTE: I could not get this to use the listing base that is set in Settings \> Permalinks properly so I am hard coding it in this snippet. Change ‘listing-base’ to whatever you want your base to be. There are four places to change this.

```auto
// Modify the permalink structure for hp_listing to include region (city) and category
function custom_hp_listing_permalink($permalink, $post) {
    // Ensure we are modifying hp_listing post type
    if ($post->post_type === 'hp_listing') {
        // Get the category terms
        $category_terms = get_the_terms($post->ID, 'hp_listing_category');
        // Get the region terms
        $region_terms = get_the_terms($post->ID, 'hp_listing_region');
        
        // Check if both taxonomies have terms
        if ($category_terms && !is_wp_error($category_terms) && 
            $region_terms && !is_wp_error($region_terms)) {
            
            $category_slug = $category_terms[0]->slug; // Get first category
            
            // Get the lowest level region (city)
            $city_term = null;
            foreach ($region_terms as $term) {
                // If term has no children, it's the lowest level
                $children = get_term_children($term->term_id, 'hp_listing_region');
                if (empty($children)) {
                    $city_term = $term;
                    break;
                }
            }
            
            // If we found a city, include it in the URL
            if ($city_term) {
                return home_url("/listing-base/{$city_term->slug}/$category_slug/$post->post_name/");
            } else {
                // Fallback to just category if no city is found
                return home_url("/listing-base/$category_slug/$post->post_name/");
            }
        }
    }
    return $permalink;
}
add_filter('post_type_link', 'custom_hp_listing_permalink', 10, 2);

// Add custom rewrite rule to match the new URL structure
function custom_hp_listing_rewrite_rules() {
    add_rewrite_rule(
        '^listing-base/([^/]+)/([^/]+)/([^/]+)/?$',
        'index.php?hp_listing=$matches[3]&hp_listing_category=$matches[2]&hp_listing_region=$matches[1]',
        'top'
    );
    
    // Add fallback rule for listings without city
    add_rewrite_rule(
        '^listing-base/([^/]+)/([^/]+)/?$',
        'index.php?hp_listing=$matches[2]&hp_listing_category=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_hp_listing_rewrite_rules');

// Flush rewrite rules when activating the theme or plugin
function flush_custom_hp_listing_rewrites() {
    custom_hp_listing_rewrite_rules();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__ , 'flush_custom_hp_listing_rewrites');
register_deactivation_hook( __FILE__ , 'flush_rewrite_rules'); // Clean up on deactivation

```

If you don’t use regions, this snippet will just add the listing category to the permalink:

NOTE: Again, I could not get this to use the listing base that is set in Settings \> Permalinks properly so I am hard coding it in this snippet. Change ‘listing-base’ to whatever you want your base to be. There are two places to change this.

```auto
// Modify the permalink structure for hp_listing to include category
function custom_hp_listing_permalink($permalink, $post) {
    // Ensure we are modifying hp_listing post type
    if ($post->post_type === 'hp_listing') {
        $terms = get_the_terms($post->ID, 'hp_listing_category');
        if ($terms && !is_wp_error($terms)) {
            $category_slug = $terms[0]->slug; // Get first category
            return home_url("/listing-base/$category_slug/$post->post_name/");
        }
    }
    return $permalink;
}
add_filter('post_type_link', 'custom_hp_listing_permalink', 10, 2);

// Add custom rewrite rule to match the new URL structure
function custom_hp_listing_rewrite_rules() {
    add_rewrite_rule(
        '^listing-base/([^/]+)/([^/]+)/?$',
        'index.php?hp_listing=$matches[2]&hp_listing_category=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_hp_listing_rewrite_rules');

// Flush rewrite rules when activating the theme or plugin
function flush_custom_hp_listing_rewrites() {
    custom_hp_listing_rewrite_rules();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__ , 'flush_custom_hp_listing_rewrites');
register_deactivation_hook( __FILE__ , 'flush_rewrite_rules'); // Clean up on deactivation

```

I am open to suggestions if there is a better way or if there is unnecessary code. I’m still learning!

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [February 18, 2025, 8:19pm UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/13 "2025-02-18T20:19:03Z")

</div>

Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![kja](https://community.hivepress.io/letter_avatar_proxy/v4/letter/k/a87d85/32.png) [@kja](https://community.hivepress.io/u/kja)\
**Post date:** [February 20, 2025, 1:05am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/14 "2025-02-20T01:05:57Z")

</div>

I cleaned up the snippet to remove unnecessary code.

Key changes made:

1. Removed the city-finding loop since WordPress handles hierarchical terms automatically
2. Removed the fallback rules since both region and category will always be present
3. Removed all the rewrite rules flushing code since WordPress handles this when saving permalinks

Here is the revised snippet to include the listing region and category for:  
[mysite.com/listing-base/region/category/listing-slug](http://mysite.com/listing-base/region/category/listing-slug)

```auto
<?php
// Modify the permalink structure for hp_listing to include region and category
function custom_hp_listing_permalink($permalink, $post) {
    // Ensure we are modifying hp_listing post type
    if ($post->post_type === 'hp_listing') {
        // Get the category and region terms
        $category_terms = get_the_terms($post->ID, 'hp_listing_category');
        $region_terms = get_the_terms($post->ID, 'hp_listing_region');
        
        // Check if both taxonomies have terms
        if ($category_terms && !is_wp_error($category_terms) && 
            $region_terms && !is_wp_error($region_terms)) {
            
            // Get first category and region (WordPress handles hierarchy automatically)
            $category_slug = $category_terms[0]->slug;
            $region_slug = $region_terms[0]->slug;
            
            return home_url("/listing-base/{$region_slug}/{$category_slug}/{$post->post_name}/");
        }
    }
    return $permalink;
}
add_filter('post_type_link', 'custom_hp_listing_permalink', 10, 2);

// Add custom rewrite rule to match the URL structure
function custom_hp_listing_rewrite_rules() {
    add_rewrite_rule(
        '^listing-base/([^/]+)/([^/]+)/([^/]+)/?$',
        'index.php?hp_listing=$matches[3]&hp_listing_category=$matches[2]&hp_listing_region=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_hp_listing_rewrite_rules');

```

Here is the revised snippet to include the listing category for:  
[mysite.com/listing-base/category/listing-slug](http://mysite.com/listing-base/category/listing-slug)

```auto
<?php
// Modify the permalink structure for hp_listing to include category
function custom_hp_listing_permalink($permalink, $post) {
    // Ensure we are modifying hp_listing post type
    if ($post->post_type === 'hp_listing') {
        $terms = get_the_terms($post->ID, 'hp_listing_category');
        if ($terms && !is_wp_error($terms)) {
            $category_slug = $terms[0]->slug;
            return home_url("/listing-base/$category_slug/$post->post_name/");
        }
    }
    return $permalink;
}
add_filter('post_type_link', 'custom_hp_listing_permalink', 10, 2);

// Add custom rewrite rule to match the URL structure
function custom_hp_listing_rewrite_rules() {
    add_rewrite_rule(
        '^listing-base/([^/]+)/([^/]+)/?$',
        'index.php?hp_listing=$matches[2]&hp_listing_category=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_hp_listing_rewrite_rules');

```

Just change ‘listing-base’ to whatever you want your base to be, then go to Settings \> Permalinks and click save to flush the rewrite rules.

---

<div class="post-metadata">

**Author:** ![system](https://community.hivepress.io/user_avatar/community.hivepress.io/system/32/13990_2.png) [@system](https://community.hivepress.io/u/system)\
**Post date:** [March 22, 2025, 1:06am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/15 "2025-03-22T01:06:29Z")

</div>

Closed on March 22, 2025, 1:06am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![unitehearts.in](https://community.hivepress.io/user_avatar/community.hivepress.io/unitehearts.in/32/9078_2.png) [@unitehearts.in](https://community.hivepress.io/u/unitehearts.in)\
**Post date:** [May 13, 2025, 7:00am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/16 "2025-05-13T07:00:47Z")

</div>

> [@Desired permalink structure using region and category](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929):
>
> Hello, I enabled generate regions from location in Geolocation settings. It’s creating the region successfully where a new listing creates the State \> County \> City hierarchy in regions. I’m trying to figure out how to structure my permalinks using regions and categories to look like this: [mysite.com/state/city/category/listing-slug](http://mysite.com/state/city/category/listing-slug) Is there a way to do this? I don’t want the county…just state and city.

I found this to be very interesting and it works well.  
One issue is there and I wanted to know how that can be solved.

If someone is not signed in and clicks the Add Listing Button, then it redirects endlessly and gives an error.

How can this be solved ?  
Maybe some extra snippet is needed for this  
Please help @ihor  
I noticed that you appreciated the coding done for this in your comment.

---

<div class="post-metadata">

**Author:** ![andrii](https://community.hivepress.io/user_avatar/community.hivepress.io/andrii/32/2951_2.png) [@andrii](https://community.hivepress.io/u/andrii)\
**Post date:** [May 14, 2025, 10:17am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/18 "2025-05-14T10:17:22Z")

</div>

Hi,

If you mean that Add Listing crashes after adding this PHP snippet [Desired permalink structure using region and category - #14 by kja](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/14), then unfortunately, we can’t help you, as we don’t debug third-party snippets.

---

<div class="post-metadata">

**Author:** ![andrii](https://community.hivepress.io/user_avatar/community.hivepress.io/andrii/32/2951_2.png) [@andrii](https://community.hivepress.io/u/andrii)\
**Post date:** [May 14, 2025, 10:17am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/19 "2025-05-14T10:17:36Z")

</div>

Opened on May 14, 2025, 10:17am UTC



---

<div class="post-metadata">

**Author:** ![unitehearts.in](https://community.hivepress.io/user_avatar/community.hivepress.io/unitehearts.in/32/9078_2.png) [@unitehearts.in](https://community.hivepress.io/u/unitehearts.in)\
**Post date:** [May 14, 2025, 10:19am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/20 "2025-05-14T10:19:42Z")

</div>

i discovered that this was not the issue. it is a clash between bookings, marketplace etc and reviews.  
without reviews plugin, things are fine.  
when i add reviews issue crops up

---

<div class="post-metadata">

**Author:** ![unitehearts.in](https://community.hivepress.io/user_avatar/community.hivepress.io/unitehearts.in/32/9078_2.png) [@unitehearts.in](https://community.hivepress.io/u/unitehearts.in)\
**Post date:** [May 14, 2025, 12:00pm UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/21 "2025-05-14T12:00:32Z")

</div>

@andrii One request. One of the most important things for a Classifieds plugin is good pemalinks as per Categories, so that it is easy to read and search.  
Could you suggest a good way to add this in permalinks ?

---

<div class="post-metadata">

**Author:** ![andrii](https://community.hivepress.io/user_avatar/community.hivepress.io/andrii/32/2951_2.png) [@andrii](https://community.hivepress.io/u/andrii)\
**Post date:** [May 15, 2025, 11:39am UTC](https://community.hivepress.io/t/desired-permalink-structure-using-region-and-category/14929/24 "2025-05-15T11:39:46Z")

</div>

Hi,

Thanks for the details. We will try to add more options for customizing permalinks, but in this case, these are WordPress-level settings. From our side, we have added options to change slugs for listings, vendors, categories, and others, but changing the complex structure where custom taxonomy is included, then this already needs to be configured at the WordPress level, as in this example: [https://wordpress.stackexchange.com/questions/39500/how-to-create-a-permalink-structure-with-custom-taxonomies-and-custom-post-types](https://wordpress.stackexchange.com/questions/39500/how-to-create-a-permalink-structure-with-custom-taxonomies-and-custom-post-types) (listing is `hp_listing post type`, category is `hp_listing_category taxonomy`, regions is `hp_listing_region taxonomy`).

I hope it helps
