Gists

4 months ago
add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
  
  if ( 'my_color_facet_name' == $facet['name'] ) { // Change 'my_color_facet_name' to the name of your facet. 
    
    $term_ids = get_terms( [
      'taxonomy' => 'pa_color', // Change 'pa_color' to the name of the attribute used in the Color facet's data source setting..
      'term_order' => true,
      'fields' => 'ids',
    ] );

    if ( ! empty( $term_ids ) && ! is_wp_error( $term_ids ) ) {
      $term_ids = implode( ',', $term_ids );
      $orderby = "FIELD(f.term_id, $term_ids)";
    }

  }
  return $orderby;
}, 10, 2 );
2 years ago
<?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 );
4 years ago
<?php

// Add to your (child) theme's functions.php

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
    if ( 'pa_variety' == $facet['name'] ) {
        $orderby = 'f.facet_display_value ASC';
    }
    return $orderby;
}, 10, 2 );
4 years ago
<?php 
/** multiple facetwp_facet_orderby rules **/
add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
    if ( 'source' == $facet['name'] ) {
        $orderby = 'FIELD( f.facet_display_value, "Second Value in Dropdown", "First Value in Dropdown" ) DESC, f.facet_display_value ASC';
    }
    return $orderby;
}, 11, 2 );
5 years ago
<?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 );
5 years ago
<?php

// Add the following to your (child) theme's functions.php

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
    if ( 'size_women' == $facet['name'] ) {
        $orderby = 'f.facet_display_value+0 ASC';
    }
    return $orderby;
}, 10, 2 );
5 years ago
<?php
/** order by facet value (value shown in url on selection) instead of display value (label) **/

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
    if ( 'day_of_week' == $facet['name'] ) {
        $orderby = 'FIELD(f.facet_value, "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday")';
    }
    return $orderby;
}, 10, 2 );
5 years ago
<?php

/* Remove "INCH" from the raw value */
add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'barrel_length' == $params['facet_name'] ) {
    
        // format the value
        $params['facet_value'] = preg_replace("/[^0-9.]/", '', $params['facet_value']);
    }
    return $params;
}, 10, 2 );


/* Sort numerically */
add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
    if ( 'barrel_length' == $facet['name'] ) {
        $orderby = 'f.facet_value+0 ASC';
    }
    return $orderby;
}, 10, 2 );
6 years ago
<?php

add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
    if ( 'my_facet' == $facet['name'] ) { // change 'my_facet' to name of your facet
		
		/** get you terms in the term_order and make a list of them in the form:
		 ** $ordered_terms = '"term_slug_1", "term_slug_2", "term_slug_3"';
		 ** note that the double quotes need to be part of the variable itself
		 ** so that that it will output a string as shown in
		 ** https://facetwp.com/documentation/facetwp_facet_orderby/ "Sort by arbitrary values" **/
        
        $orderby = 'FIELD(f.facet_value, $ordered_terms)';
    }
    return $orderby;
}, 10, 2 );
1 year ago
<?php
/**
 * custom sort order for a facet, this orders by facet_display_value but facet_value could also be used
 **/
add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
	if ( 'colors' == $facet['name'] ) { // change colors to the name of the facet
		$orderby = "FIELD(f.facet_display_value, 'Blue','Green','Red', 'Gray')";
	}
	return $orderby;
}, 10, 2 );