facetwp sort ACF taxonomy relationship fields by term order of the tax

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php
/**
* order facets by term order
* note: facets are an ACF relationship field
* rather than a direct taxonomy field which
* prevents use of taxomomy order directly with get_terms_orderby
*/
add_filter( ‘facetwp_facet_orderby’, function( $orderby, $facet ) {
// use ‘facet_name’ => ‘taxonomy slug’
$ordered_facets = [
‘facet_one’ => ‘tax_one’,
‘facet_two’ => ‘tax_two’
];
if ( $ordered_facets[$facet[‘name’]] ) {
$terms = get_terms( array(
‘taxonomy’ => $ordered_facets[$facet[‘name’]],
‘hide_empty’ => false,
‘orderby’ => ‘term_order’,
‘order’ => ‘ASC’
));
foreach ($terms AS $term ) {
$termlist[] = $term->slug;
}
$orderby = ‘FIELD(f.facet_value, “‘ . implode( ‘”, “‘, $termlist ) . ‘” )’;
}
return $orderby;
}, 11, 2);
{{/gist}}