Skip to content

Instantly share code, notes, and snippets.

@djrmom
djrmom / custom-hooks.php
Created July 30, 2018 21:36
facetwp layout builder with acf repeater subfields
<?php
/** 'author' is Unique name set in advanced tab of layout builder settings for the item,
** loop over the array returned from ACF to build a html string from the subfields and return to layout builder's
** facetwp_builder_item_value filter
**/
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 'author' == $item['settings']['name'] ) {
$value = '';
$authors = get_field( 'author' );
@djrmom
djrmom / readme.txt
Last active March 2, 2024 17:53
facetwp radio button style as rectangular color button
Radio button facets are actual divs rather than radio input type html.
Radio facet as rectangular buttons - https://d.pr/i/cnyjEg
Facet settings for demo - https://d.pr/i/C5lAsc
If you want to add an "All" button - https://gist.github.com/djrmom/5870f7baf38fd5df40ee861bb95a01d5
@djrmom
djrmom / custom-hooks.php
Created October 27, 2017 13:28
facetwp date source converted to year
<?php
/**
* reindex after adding or updating this filter
*/
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'date_as_year' == $params['facet_name'] ) { // change date_as_year to name of your facet
$raw_value = $params['facet_value'];
$params['facet_value'] = date( 'Y', strtotime( $raw_value ) );
$params['facet_display_value'] = $params['facet_value'];
@djrmom
djrmom / query_args.php
Created June 12, 2018 19:16
facetwp bookings query
<?php
return array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_wc_booking_availability',
'compare' => 'EXISTS'
)
)
);
@djrmom
djrmom / custom-hooks.php
Created May 18, 2018 13:11
facetwp modify facet count
<?php
/** removes () from count, could also be used to replace with alternate bracketing or other output **/
add_filter( 'facetwp_facet_html', function( $output, $params ) {
if ( 'product_categories' == $params['facet']['name'] ) {
$output = preg_replace( '/\(([0-9]+)\)/', '$1', $output );
}
return $output;
}, 10, 2 );
@djrmom
djrmom / custom-hooks.php
Created October 29, 2018 21:10
facetwp conditional scroll to top only on pager
<?php
/** scoll on loaded only if pager is the change in facet **/
add_action( 'wp_head', function() { ?>
<script>
(function($) {
$(document).on('facetwp-refresh', function() {
if ( FWP.soft_refresh == true ) {
FWP.enable_scroll = true;
@djrmom
djrmom / custom-hooks.php
Last active June 20, 2022 15:14
facetwp reset facets on search
<?php
/** reset a facet (facet 2) on submission of different facet (facet 1)
** change 'search' to name of facet 1
** change 'product_categories' to the name of the facet 2
**/
add_action( 'wp_footer', function() {
?>
<script>
(function($) {
@djrmom
djrmom / custom-hooks.php
Last active March 29, 2022 13:49
facetwp index only 1st level children in taxonomy
<?php
/** index only first level children terms, no grandchildren or deeper **/
add_filter( 'facetwp_index_row', function( $params ) {
if ( 'product_categories' == $params['facet_name'] ) { //change 'product_categories' to name of your facet
$parents = get_ancestors( $params['term_id'], 'product_cat', 'taxonomy' ); // change 'product_cat' to name of your taxonomy
if ( count( $parents ) !== 1 ) { // adjust comparison as needed for different child levels
$params['facet_value'] = ''; // skip indexing
}
}
return $params;
@djrmom
djrmom / custom-hooks.php
Created November 15, 2018 00:17
facetwp woocommerce _product_attributes
<?php
/** some types of product attributes for woocommerce (those that are not taxonomies or used for variations
** are saved only in the _product_attributes custom field as an array
** select "_product_attributes" as the datasource in the facet and use facetwp_index_row to find and index
** the correct attributes
**/
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'attributes' == $params['facet_name'] ) { //change 'attributes' to name of your facet
$values = maybe_unserialize( $params['facet_value'] );
@djrmom
djrmom / custom-hooks.php
Created December 10, 2019 13:59
facetwp order by facet_value
<?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 );