Thanks @cotasson
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.
// 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.
// 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!