wordpressfacetwp

FacetWP filter from custom field checkbox


I got a product custom field named organic with the values yes/no. I want to show a checkbox in our filter that says "Organic". And when checked search for yes values in db. The deafult setup of a checkbox in FacetWP shows the values from db.

How do I set that up in FacetWP?


Solution

  • here is your code for this task, do not forget to re-index after each name change.

    /**
     * reindex after adding or updating this filter
     */
    add_filter( 'facetwp_index_row', function( $params, $class ) {
        if ( 'new_facet' == $params['facet_name'] ) { // change my_facet to your facet's name/slug
            if ( '1' == $params['facet_value'] ) { // be careful with 1 and 0 as values - https://www.php.net/manual/en/types.comparisons.php
                $params['facet_display_value'] = 'Organic'; // text to display in the facet choices
                }
            if ( '0' == $params['facet_value'] ) {
                $params['facet_display_value'] = 'McDonalds';
                }
        }
        return $params;
    }, 10, 2 );
    

    enter image description here