Gists

by djrmom - 1 month 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 );
by mgibbs189 - 1 month 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 );

by djrmom - 2 months 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 );
by mgibbs189 - 11 months 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 );

by djrmom - 2 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 );
by djrmom - 2 years ago

/**
 * 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 );
by mgibbs189 - 3 years ago

<?php

function fwp_term_orderby( $orderby, $facet ) {
    if ( 'YOUR_FACET_NAME' == $facet['name'] ) { // Replace "YOUR_FACET_NAME"
        $term_ids = get_terms( array(
            'taxonomy' => str_replace( 'tax/', '', $facet['source'] ),
            '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;
}

add_filter( 'facetwp_facet_orderby', 'fwp_term_orderby', 10, 2 );
by mgibbs189 - 4 years ago

<?php

// PUT THIS IN YOUR THEME'S FUNCTIONS.PHP
// Replace "my_facet" with your actual facet name

function facet_custom_sort( $orderby, $facet ) {
    if ( 'my_facet' == $facet['name'] ) {
        $orderby = 'FIELD(f.facet_display_value, "Under $50", "$50 - $75", "$75 - $100", "Over $100")';
    }
    return $orderby;
}
add_filter( 'facetwp_facet_orderby', 'facet_custom_sort', 10, 2 );