Filter location dropdowns to only show active/pending property locations
- Add homeproz_get_active_locations() helper function - Only shows locations that have properties with Active or Pending status - Applied to homepage community dropdown and properties page location filter - Sold properties no longer count toward location visibility 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -228,3 +228,69 @@ function homeproz_get_option($key, $default = '') {
|
|||||||
|
|
||||||
return isset($options[$key]) ? $options[$key] : $default;
|
return isset($options[$key]) ? $options[$key] : $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get property locations that have active or pending properties
|
||||||
|
*
|
||||||
|
* @return array Array of term objects
|
||||||
|
*/
|
||||||
|
function homeproz_get_active_locations() {
|
||||||
|
// Get Active and Pending status term IDs
|
||||||
|
$active_term = get_term_by('slug', 'active', 'property_status');
|
||||||
|
$pending_term = get_term_by('slug', 'pending', 'property_status');
|
||||||
|
|
||||||
|
$status_ids = array();
|
||||||
|
if ($active_term) $status_ids[] = $active_term->term_id;
|
||||||
|
if ($pending_term) $status_ids[] = $pending_term->term_id;
|
||||||
|
|
||||||
|
if (empty($status_ids)) {
|
||||||
|
// Fallback: return all non-empty locations if no status terms exist
|
||||||
|
return get_terms(array(
|
||||||
|
'taxonomy' => 'property_location',
|
||||||
|
'hide_empty' => true,
|
||||||
|
'orderby' => 'name',
|
||||||
|
'order' => 'ASC',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query properties with Active or Pending status
|
||||||
|
$properties = get_posts(array(
|
||||||
|
'post_type' => 'property',
|
||||||
|
'posts_per_page' => -1,
|
||||||
|
'fields' => 'ids',
|
||||||
|
'tax_query' => array(
|
||||||
|
array(
|
||||||
|
'taxonomy' => 'property_status',
|
||||||
|
'field' => 'term_id',
|
||||||
|
'terms' => $status_ids,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
|
||||||
|
if (empty($properties)) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get unique location terms from these properties
|
||||||
|
$location_ids = array();
|
||||||
|
foreach ($properties as $property_id) {
|
||||||
|
$terms = wp_get_post_terms($property_id, 'property_location', array('fields' => 'ids'));
|
||||||
|
if (!is_wp_error($terms)) {
|
||||||
|
$location_ids = array_merge($location_ids, $terms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$location_ids = array_unique($location_ids);
|
||||||
|
|
||||||
|
if (empty($location_ids)) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the actual term objects
|
||||||
|
return get_terms(array(
|
||||||
|
'taxonomy' => 'property_location',
|
||||||
|
'include' => $location_ids,
|
||||||
|
'orderby' => 'name',
|
||||||
|
'order' => 'ASC',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|||||||
@@ -43,15 +43,10 @@ $size_class = $size === 'small' ? 'hero-section--small' : 'hero-section--large';
|
|||||||
// Prepare gallery data attribute for JS
|
// Prepare gallery data attribute for JS
|
||||||
$gallery_data = !empty($gallery_images) ? esc_attr(wp_json_encode($gallery_images)) : '';
|
$gallery_data = !empty($gallery_images) ? esc_attr(wp_json_encode($gallery_images)) : '';
|
||||||
|
|
||||||
// Get locations for dropdown if needed
|
// Get locations for dropdown if needed (only locations with active/pending properties)
|
||||||
$locations = array();
|
$locations = array();
|
||||||
if ($show_location_search) {
|
if ($show_location_search) {
|
||||||
$locations = get_terms(array(
|
$locations = homeproz_get_active_locations();
|
||||||
'taxonomy' => 'property_location',
|
|
||||||
'hide_empty' => false,
|
|
||||||
'orderby' => 'name',
|
|
||||||
'order' => 'ASC',
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ $current_sort = isset($_GET['sort']) ? sanitize_text_field($_GET['sort']) : 'new
|
|||||||
// Get taxonomy terms
|
// Get taxonomy terms
|
||||||
$property_types = get_terms(array('taxonomy' => 'property_type', 'hide_empty' => false));
|
$property_types = get_terms(array('taxonomy' => 'property_type', 'hide_empty' => false));
|
||||||
$property_statuses = get_terms(array('taxonomy' => 'property_status', 'hide_empty' => false));
|
$property_statuses = get_terms(array('taxonomy' => 'property_status', 'hide_empty' => false));
|
||||||
$property_locations = get_terms(array('taxonomy' => 'property_location', 'hide_empty' => false));
|
$property_locations = homeproz_get_active_locations();
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<div class="property-filters" id="property-filters" data-ajax-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>">
|
<div class="property-filters" id="property-filters" data-ajax-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>">
|
||||||
|
|||||||
Reference in New Issue
Block a user