Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Custom field in search settings to appear dynamically › Reply To: Custom field in search settings to appear dynamically
July 30, 2020 at 10:12 am
#28757
Keymaster
I’m sorry, I may have phrased incorrectly.
The code I gave does not resolve the issue – it is only a sample code to remove values from a frontend filter, based on the custom field name.
The $exclude variable needs to be filled with the values to exclude. That section needs to be custom coded. Perhaps to do a query on the meta table with a left join to get the union of meta fields that exist in both sets. Something like
add_filter('asp_pre_get_front_filters', 'asp_change_a_filter', 10, 2);
function asp_change_a_filter($filters, $type) {
// Enter the custom field name
$field = 'winery';
// Get the custom field values to exclude from the filter
global $wpdb;
$exclude = $wpdb->get_results("
SELECT DISTINCT(pm.meta_value) FROM $wpdb->postmeta pm
LEFT JOIN $wpdb->postmeta ppm ON (pm.post_id = ppm.post_id AND ppm.meta_key = '_stock_status' AND ppm.meta_value = 'instock')
WHERE pm.meta_key = '$field'
", ARRAY_N);
foreach ($filters as $k => $filter) {
if ( $filter->type() == 'custom_field' && $filter->data['field'] == $field ) {
$filter->remove($exclude);
}
}
return $filters;
}
This should be really really close to an actual solution.