<?php
/**
** creates arbitrary values orderby string to sort facets
** in order of modifier values in include modifer setting of facet
** see https://facetwp.com/help-center/developers/hooks/querying-hooks/facetwp_facet_orderby/#sort-by-arbitrary-values
**/
add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
if ( 'my_categories' == $facet['name'] ) {
if ( ! empty( $facet['modifier_values'] ) ) {
$temp = preg_split( '/\r\n|\r|\n/', trim( $facet['modifier_values'] ) );
$values = [];
// Compare using both original and encoded values
foreach ( $temp as $val ) {
$val = trim( $val );
$val_encoded = htmlentities( $val );
$val_decoded = html_entity_decode( $val );
$values[ $val ] = true;
$values[ $val_encoded ] = true;
$values[ $val_decoded ] = true;
}
$values = implode( '","', array_keys( $values ) );
$orderby = 'FIELD(f.facet_display_value, "' . $values . '")';
}
}
return $orderby;
}, 10, 2 );