Skip to content

Instantly share code, notes, and snippets.

@djrmom
djrmom / facet.js
Last active September 15, 2018 11:25
facetwp - click event to reset individual facet
(function($) {
$(document).on('click', '#reset', function() {
FWP.is_reset = true;
FWP.facets['my_facet'] = []; // set facet to no selections
delete FWP.facets['paged']; // remove "paged" from URL
FWP.refresh();
});
})(jQuery);
@djrmom
djrmom / custom-hooks.php
Last active August 25, 2020 16:01
facetwp fselect only parent terms
<?php add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'my_facet' == $params['facet_name'] ) { // change my_facet
if ( !empty( $params['parent_id'] ) ) {
$params['facet_value'] = ''; // skip indexing
}
}
return $params;
}, 10, 2 );
@djrmom
djrmom / custom-hooks.php
Created August 24, 2017 14:51
facetwp add view all radio button
<?php
/** adds a "View All" index value to every post
** change 'my_radio' to the name of the radio facet to apply to
** 'View All' can be changed to whatever label is preferred
** reindex after add this code
**/
add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
if ( 'my_radio' == $params['facet']['name'] ) {
$new_row = $params['defaults'];
@djrmom
djrmom / custom-hooks.php
Last active August 25, 2020 16:01
facetwp multiple parents
<?php
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'sub_categories' == $params['facet_name'] ) { // change sub_categories to name of your facet
$parents = array( 18, 20 ); // change to ids of your parent terms
if ( ! isset( $params['parent_id'] ) || ! in_array( $params['parent_id'], $parents ) ) {
$params['facet_value'] = ''; // skip indexing
}
}
return $params;
@djrmom
djrmom / custom-hooks.php
Created August 28, 2017 13:58
facetwp make select sort into a radio
<?php
/**
* filter html for sort to output radio buttons
*/
add_filter( 'facetwp_sort_html', function( $output, $params ) {
$output = '<div class="facetwp-sort-radio">';
foreach ( $params['sort_options'] as $key => $atts ) {
$output .= '<input type="radio" name="sort" value="' . $key . '"> ' . $atts['label'] . '<br>';
}
$output .= '</div>';
@djrmom
djrmom / custom-hooks.php
Created August 28, 2017 15:42
facetwp custom source for lat/long
<?php
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'product_proximity' == $params['facet_name'] ) {
$latlng = $params['facet_display_value'];
if ( is_string( $latlng ) ) {
$latlng = preg_replace( '/[^0-9.,-]/', '', $latlng );
if ( preg_match( "/^([\d.-]+),([\d.-]+)$/", $latlng ) ) {
@djrmom
djrmom / custom-hooks.php
Created August 29, 2017 21:02
facetwp add a taxonomy type source
<?php
/**
* add additional source to select as Taxonomy Types
*/
add_filter( 'facetwp_facet_sources', function( $sources ) {
$sources['posts']['choices']['tax_type'] = 'Taxonomy Types';
return $sources;
@djrmom
djrmom / custom-hooks.php
Last active August 25, 2020 16:01
facetwp index only direct children of a parent term
<?php
/**
** filter on facetwp_index_row to index only direct children of a parent term
** this is for use with Parent term setting which by default indexes child terms, and grand child terms, etc
** If you want to have the index include posts of child/grandchild, etc of terms, you need to set Hierarchical to yes
**/
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( in_array( $params['facet_name'], array( 'my_facet', 'my_other_facet' ) ) ) { // create an array of facet names to check against
$parent = $class->facet['parent_term']; // this gives the parent term from the facet's settings
@djrmom
djrmom / facet.js
Last active August 31, 2017 23:05
facetwp disable dropdown conditionally
<script>
(function($) {
/**
* creates a parent -> child -> grandchild dependency to disable facets
*/
$(document).on('facetwp-loaded', function() {
if ('undefined' !== typeof FWP.facets['product_categories'] && FWP.facets['product_categories'].length < 1 ) {
$( '.facetwp-facet-product_tags select, .facetwp-facet-rating select').prop('disabled', 'disabled');
} else if ('undefined' !== typeof FWP.facets['product_tags'] && FWP.facets['product_tags'].length < 1 ) {
$( '.facetwp-facet-rating select').prop('disabled', 'disabled');
@djrmom
djrmom / facet.css
Last active November 19, 2019 03:56
facetwp css classes
/**
** facet classes
**/
.facetwp-facet {
/* class of div that wraps each facet type */
}
.facetwp-facet-FACET-NAME {
/* class for a facet where facet name is FACET-NAME, applied to same div as .facetwp-facet */
}