Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
facetwp order by acf order of choices when term order is the sort
<?php
/** use term order sort setting to sort by order of choices set in ACF field **/
add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
if ( isset( $facet['source'] ) && 'acf/' == substr( $facet['source'], 0, 4 ) && 'term_order' == $facet['orderby'] ) {
$source = str_replace( 'acf/', '', $facet['source'] );
if ( isset( get_field_object( $source )['choices'] ) ) {
$ordered_choices = implode( '", "', get_field_object( $source )['choices'] );
$orderby = 'FIELD(f.facet_display_value, "' . $ordered_choices . '")';
}
}
return $orderby;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.