Desired permalink structure using region and category

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

<?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

<?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.

1 Like