Skip to content

Instantly share code, notes, and snippets.

@propertyhive
Last active November 11, 2024 03:25
Show Gist options
  • Save propertyhive/3002a86bde7cdba893730eabbb7e6f18 to your computer and use it in GitHub Desktop.
Save propertyhive/3002a86bde7cdba893730eabbb7e6f18 to your computer and use it in GitHub Desktop.
add_filter( 'propertyhive_search_form_fields_after', 'edit_property_search_form_fields' );
function edit_property_search_form_fields($fields)
{
global $wpdb;
$options = array('' => 'No Preference');
// Query to get distinct meta values for the '_building' meta key
$results = $wpdb->get_col(
$wpdb->prepare(
"
SELECT DISTINCT pm.meta_value
FROM {$wpdb->postmeta} pm
INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
INNER JOIN {$wpdb->postmeta} pm2 ON pm.post_id = pm2.post_id
WHERE pm.meta_key = %s
AND p.post_type = %s
AND p.post_status = 'publish'
AND pm2.meta_key = %s
AND pm2.meta_value = %s
",
'_building', // Meta key for building
'property', // Post type
'_on_market', // Meta key to check
'yes' // Value to match
)
);
// Print the distinct values
if (!empty($results)) {
foreach ($results as $building) {
$options[$building] = $building;
}
}
$fields['_building']['options'] = $options;
return $fields; // return the fields
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment