Gists

20 hours ago
<?php
// Hides a facet if there is only one option left
// Replace 'my_facet_name' with the name of your facet (4x)
add_action( 'facetwp_scripts', function() { ?>
  <script>
    (function($) {
      document.addEventListener('facetwp-loaded', function() {
        if ( FWP.settings.num_choices.my_facet_name !== 'undefined' && FWP.settings.num_choices.my_facet_name < 2 ) {
          $('.facetwp-facet-my_facet_name').addClass( 'facetwp-hidden' );
        } else {
          $('.facetwp-facet-my_facet_name').removeClass( 'facetwp-hidden' );
        }
      });
    })(fUtil);
  </script>
<?php } );
1 week ago
<?php

// Combines specific choices in a facet that has its data source set to a 'farbe' (color) global product attribute.
// The value is combined with another global attribute 'holzart' (wood type) for certain holzart-farbe combinations.
// E.g. products with the global attributes 'Eiche' and 'Farblos', and 'Buche' and 'Farblos', need to be indexed as 'NATUR'.
// All other combinations need to be indexed with the holzart and farbe values, e.g. 'Esche Farblos'.
// Note: this works for simple products, not variation products.

add_filter('facetwp_index_row', function ($params, $class) {

  if ('farbigkeit' == $params['facet_name']) {

    $product_id = $params['post_id'];
    $pa_farbe_name = $params['facet_display_value']; // The facet has data source 'pa_farbe' already, so it alrady has the display value

    // Get the global attribute term name  
    $product = wc_get_product( $product_id );
    $product_attributes = $product->get_attributes();
    $pa_holzart_id = $product_attributes['pa_holzart']['options']['0']; // returns the ID of the term
    $pa_holzart_name = get_term($pa_holzart_id)->name; // returns the term name

    // Custom logic for combining "Holzart" and "Farbe" into "NATUR"
    if ( ('Eiche' == $pa_holzart_name && 'Farblos' == $pa_farbe_name) || ('Buche' == $pa_holzart_name && 'Farblos' == $pa_farbe_name) ) {
      $combined_display_value = 'NATUR';
      $combined_value = 'natur';
    } else {
      $combined_display_value = $pa_holzart_name . ' ' . $pa_farbe_name; // Set the display value as e.g. 'Esche Farblos'
      $combined_value = str_replace(' ', '_', strtolower($combined_display_value)); // Set the technical value as e.g. esche_farblos (lowercase and spaces replaced with underscores)
    }

    $params['facet_display_value'] = $combined_display_value;
    $params['facet_value'] = $combined_value;

  }
  return $params;
}, 10, 2);
2 weeks ago
<?php
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
    if ( $is_main_query && $query->is_main_query() && $query->is_search() ) {
        if ( true == $query->get( 'wc_query' ) ) {
            $query->set( 'using_searchwp', '' );
        }        
    }
    return $is_main_query;
}, 11, 2);
2 weeks ago
<?php
/** shows prev link while on page 1 and next link while on last page
 **/
add_filter( 'facetwp_facet_html', function( $output, $params ) {
    if ( 'standard_pager' == $params['facet']['name'] ) { // change 'standard_pager' to name of your pager facet
        $page = FWP()->facet->pager_args['page'];
        $total = FWP()->facet->pager_args['total_pages'];
        if ( 2 > $page ) { // if this is page 1
            // add the prev page link
            $label = facetwp_i18n( $params['facet']['prev_label'] );
            $class = 'facetwp-page prev active';
            $data = ' data-page="1"';
            $html = '<a class="' . $class . '"' . $data . '>' . $label . '</a>';
            $output = str_replace( '<div class="facetwp-pager">', '<div class="facetwp-pager">' . $html, $output );           
        } elseif ( $page == $total) { // if this is last page
            // add the next page link
            $label = facetwp_i18n( $params['facet']['next_label'] );
            $class = 'facetwp-page next active';
            $data = ' data-page="' . $page .'"';
            $html = '<a class="' . $class . '"' . $data . '>' . $label . '</a>';
            $output = str_replace( '</div>', $html . '</div>', $output ); // 
        }
    }
    return $output;
}, 10, 2 );
  
3 weeks ago
<?php
// Resets a facet on first page load only, if it has a selection in the URL. 
// Can be used if users have bookmarked links with now non-existing facet choices.
// Retains other facet choices.

// Part 1: Reset the facet choice by preloading the empty url vars for it on first page load
add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
  if ( 'demo/cars' == FWP()->helper->get_uri() ) { // Replace 'demo/cars' with the URI of your page (everything after the domain name, excluding any slashes at the beginning and end)
    if ( ! empty( $url_vars['myfacetname'] ) ) { // Replace 'myfacetname' with the name of your facet
      $url_vars['myfacetname'] = []; // Replace 'myfacetname' with the name of your facet
    }
  }
  return $url_vars;
} );

// Part 2: Remove the facet's URL vars for the facet
add_action( 'facetwp_scripts', function() {
  ?>
  <script>
    document.addEventListener('facetwp-refresh', function() {
      if ( ! FWP.loaded ) { // Only on first page load
        var facet_name = 'myfacetname';
        if ( 'object' == typeof FWP.facets[facet_name] && FWP.facets[facet_name].length  ) {
          FWP.facets[facet_name] = []; // Remove choices
          FWP.setHash(); // Reset the URL vars
        }
      }
    });
  </script>
  <?php
}, 100 );
3 weeks ago
<?php
/** this error occurs when there are no available dates for the calendar picker
 ** to display, changing from 0 to "" fixes the calendar but 
 ** actual dates could be used for min/max if preferred, see
 ** https://facetwp.com/help-center/developers/hooks/output-hooks/facetwp_render_output/#customize-a-date-range-facets-date-picker-range
 ** all 4 of these checks may not be needed if both dates are
 ** to configured to display
 **/

// Replace "bad_dates" with the name of your Date Range facet
add_filter( 'facetwp_render_output', function( $output, $params ) {

    if ( isset( $output['settings']['bad_dates'] ) ) {
        if ( 0 == $output['settings']['bad_dates']['range']['min']['minDate'] ) {
            $output['settings']['bad_dates']['range']['min']['minDate'] = ''; // start date min
        }
        if ( 0 == $output['settings']['bad_dates']['range']['min']['maxDate'] ) {
            $output['settings']['bad_dates']['range']['min']['maxDate'] = ''; // start date max
        }
        if ( 0 == $output['settings']['bad_dates']['range']['max']['minDate'] ) {
            $output['settings']['bad_dates']['range']['max']['minDate'] = ''; // End date min
        }
        if ( 0 == $output['settings']['bad_dates']['range']['max']['maxDate'] ) {
            $output['settings']['bad_dates']['range']['max']['maxDate'] = ''; // End date max
        }
    }

    return $output;
  }, 10, 2 );
3 weeks ago
<?php
// Also see: https://gist.facetwp.com/gist/fake-facet-choices/

add_filter( 'facetwp_facet_html', function( $output, $params ) {

  if ( 'event_month' == $params['facet']['name'] ) { // adjust name of facet

    $values = (array) $params['values'];
    $selected_values = (array) $params['selected_values'];

    for ( $x = 0; $x < 12; $x ++ ) {
      $time = strtotime( '+' . $x . ' months', strtotime( date( 'Y-M' . '-01' ) ) );
      $key = date( 'Y-m', $time );
      $name = date( 'F', $time );
      $months[ $key ] = $name;
    }

    if ( empty( $selected_values ) ) {
      $fake_output = '<div class="facetwp-radio checked" data-value="">All</div>';
    } else {
      $fake_output = '<div class="facetwp-radio" data-value="">All</div>';
    }

    foreach ( $months as $fake => $fake_display ) {
      $key = array_search( $fake, array_column( $values, 'facet_value' ) );
      if ( ! empty( $key ) || $key === 0 ) {
        $selected = in_array( $values[ $key ]['facet_value'], $selected_values ) ? ' checked' : '';
        $fake_output .= '<div class="facetwp-radio' . $selected . '" data-value="' . $fake . '"><span class="facetwp-display-value">' . $values[ $key ]['facet_display_value'] . '</span></div>';
      } else {
        $fake_output .= '<div class="facetwp-radio disabled" data-value="' . $fake . '"><span class="facetwp-display-value">' . $fake_display . '</span></div>';
      }
    }
    $output = $fake_output;
  }
  return $output;

}, 10, 2 );
3 weeks ago
<?php

// Set your Post Author Listing Builder item's 'Author field' setting to 'User ID'.
// Replace 'my-author-item' with the 'Unique name' of your Post Author builder item.

add_filter( 'facetwp_builder_item_value', function( $value, $item ) {

  if( $item['settings']['name'] == 'my-author-item' ) {

    $author_id = $value;
    $author_link = get_author_posts_url( $author_id );
    $author_name = get_the_author_meta( 'display_name', $author_id );

    return '<a href="' . $author_link . '">'. $author_name . '</a>';
    
  }
  return $value;

}, 10, 2 );
3 weeks ago
<?php

// Creates a custom dynamic tag {{ childterms }} to display post terms of a specific taxonomy in a Listing Builder (HTML) item.
// Excludes top-level parent terms, so shows child terms only.
// Displays the term names in a comma-separated list.
// Replace 'my_taxonomy' with the name of your taxonomy.
// Use the custom dynamic tag in a (HTML) builder item like this: {{ childterms }}


add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {
  if ( 'childterms' == $tag_name ) {
    $taxonomy = 'my_taxonomy';
    $terms = wp_get_post_terms( $params['post']->ID,  $taxonomy );
    $child_terms = array();

    foreach ( $terms as $term ) {
      $parent_id = $term->parent;
      if ( $parent_id !== 0 ) { // Exclude top-level parent terms (that have a parent ID of 0).
        $child_terms[] = $term->name; // Get term names
      }
    }
    $tag_value = implode( ', ', $child_terms ); // Output the term names as a comma-separated list

  }
  return $tag_value;
}, 10, 3 );
4 weeks ago
<?php

/** meta query in facet's listing builder query dev mode for tribe upcoming events
 ** when the tribe_event cpts is combined with other post types
 ** only for non-recurring events
 **/


"meta_query" => [
    "relation" => "OR",
    [
      "key" => "_EventStartDate",
      "compare" => ">",
      "value" => "date|now"
    ],
    [
      "key" => "_EventStartDate",
      "compare" => "NOT EXISTS"
    ]
],