Skip to content

Instantly share code, notes, and snippets.

@dennisenderink
Created January 21, 2015 10:11
Show Gist options
  • Save dennisenderink/ac24a3d044271f30d994 to your computer and use it in GitHub Desktop.
Save dennisenderink/ac24a3d044271f30d994 to your computer and use it in GitHub Desktop.
FacetWP ACF custom field options
<?php
add_filter('facetwp_facet_html', 'de_acf_facetwp_options', 10, 2);
function de_acf_facetwp_options($output, $params)
{
// map field key
$fieldKeys = [
'custom_field_name_1' => 'field_{key}',
];
// get clean custom field source
$source = str_replace('cf/', '', $params['facet']['source']);
if (in_array($params['facet']['type'], ['checkboxes', 'dropdown']) && preg_match('/^cf\//', $params['facet']['source']) && in_array($source, array_keys($fieldKeys)) && $object = get_field_object($fieldKeys[$source])) {
// create selectbox
$select = '<select class="facetwp-dropdown">';
$select .= '<option value>' . $params['facet']['label_any'] . '</option>';
foreach ($params['values'] as $value) {
$select .= '<option value="' . $value->facet_value . '">' . $object['choices'][$value->facet_value] . ' (' . $value->counter . ')</option>';
}
$select .= '</select>';
return $select;
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment