Gists

5 days ago
<?php
// Disable FacetWP's query variables in the URL, except for Pager facets.
// To entirely disable FacetWP's query variables, see:
// https://facetwp.com/help-center/developers/the-facetwp-url/#disable-facetwps-query-string

add_action( 'facetwp_scripts', function() {
  ?>
  <script>
    document.addEventListener('facetwp-refresh', function() {

        FWP.buildQueryString = function() {

          var fwp_vars = Object.assign({});

          // Add pagination to the URL hash
          if (1 < FWP.paged) {
            fwp_vars['paged'] = FWP.paged;
          }

          fwp_vars = FWP.helper.serialize(fwp_vars, FWP_JSON.prefix);
          return fwp_vars;
        }

    });
  </script>
  <?php
}, 100 );
2 weeks ago
<?php
add_action('facetwp_scripts', function () { ?>
  <script>
    (function($) {
      $().on('facetwp-loaded', function() {
        var has_posts = ( 0 <  FWP.settings.pager.total_rows);
        var method = has_posts ? 'removeClass' : 'addClass';
        $('h1.listing-title')[method]('facetwp-hidden');
      });
    })(fUtil);
  </script>
<?php }, 100);
1 month ago
<?php

// See: https://facetwp.com/help-center/developers/javascript-reference/facetwp-refresh/#reset-all-other-facets-on-change-of-any-facet
// Or, to reset on change of a specific facet:
// https://facetwp.com/help-center/developers/javascript-reference/facetwp-refresh/#reset-all-other-facets-on-change-of-a-specific-facet

add_action( 'facetwp_scripts', function() { ?>
  <script>
    document.addEventListener('facetwp-refresh', function() {
      if ( null !== FWP.active_facet ) {
        let current_facet = fUtil(FWP.active_facet.nodes[0]).attr('data-name' );
        let others = FWP.facets;
        Object.keys(others).forEach(function (key) {
          if ( current_facet != key ) {
            FWP.facets[key] = [];
          }
        });
      }
    });
  </script>
<?php }, 100 );
1 month ago
<?php

// Get the minimum and maximum indexed values for the posts in the current query on the page 
// for a specific number-based facet like a Number Range facet

global $wpdb;

$facet_name = 'my_facet_name'; // replace with name of your facet
$post_ids = FWP()->unfiltered_post_ids;
$sql = "
SELECT MIN(facet_value + 0) AS `min`, MAX(facet_display_value + 0) AS `max` FROM {$wpdb->prefix}facetwp_index
WHERE facet_name = '{$facet_name}' AND facet_display_value != ''  AND post_id IN (' . implode( ',', $post_ids ) . ')';";

$row = $wpdb->get_row( $sql );

$range_min = (float) $row->min;
$range_max = (float) $row->max;
2 months ago
<?php
/** scrolls to top on facet interaction
 ** except for load more pager
 **/
add_action('facetwp_scripts', function () {
?>
    <script>
        (function($) {
            $(document).on('facetwp-loaded', function() {
                if (FWP.loaded && FWP.load_more_paged < 2) { // Run only after the initial page load
                    $('html, body').animate({
                        scrollTop: $('.facetwp-template').offset().top // Scroll to the top of the element with class "facetp-template"
                    }, 500);
                }
            });
        })(jQuery);
    </script>
<?php });
2 months ago
<?php
/** allows magnifying glass search icon 
 ** to trigger a submit even when auto_refresh
 ** is disabled
 **/
add_action('facetwp_scripts', function () {
    ?>
    <script>
        (function($) {
            $().on('click', '.facetwp-type-search .facetwp-icon', function() {
                if (! FWP.is_refresh) {
                    FWP.refresh();
                }
            });
        })(fUtil);
    </script>
<?php
});
2 months ago
<?php
/** use woocommerce_get_product_thumbnail function for featured images
 ** in layout builder when post type is product
 **/
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
	if ( 'featured_image' == $item['source'] && 'product' == get_post_type() ) { 
		$value = woocommerce_get_product_thumbnail();
	}
	return $value;
}, 10, 2 );
2 months ago
<?php
/** deletes post-queries cache keys before get so that they
 ** have to be set again
 ** only for using object cache with memcache and automattic memcached plugin
 ** https://github.com/Automattic/wp-memcached/
 **/
add_filter( 'pre_wp_cache_get', function( $value, $key, $group, $force ) {
    if ( function_exists( 'FWP' ) && is_object( FWP()->helper ) && 'blog' == FWP()->helper->get_uri() && // change 'blog' to page facet is used on
        "post-queries" == $group  && false === strpos( $key, 'get_page_by_path' ) ) {
			// delete this cache key
            wp_cache_delete( $key, "post-queries" );

    }
    return $value;
}, 10, 4 );
2 months ago
<?php
// See: https://facetwp.com/how-to-filter-wp-attachments-and-draft-pending-or-private-posts/#display-only-certain-types-of-attachments

// The following will work if the query ONLY retrieves the 'attachment' post type:

$unsupported_mimes = array( 'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'image/x-icon' );
$all_mimes = get_allowed_mime_types();
$accepted_mimes = array_diff( $all_mimes, $unsupported_mimes );

$args = array(
  'post_type'      => [ 'attachment' ],
  'post_status' => [ 'publish', 'inherit' ], // 'inherit' is needed for attachments
  'posts_per_page' => 15, 
  'post_mime_type' => $accepted_mimes // only allow attachments that are not images
);


// The above will NOT work if there are also OTHER post types than attachments in the query, like posts or pages.
// In this case you need to use 'post_where' as shown below. See: https://wordpress.stackexchange.com/a/209720

add_filter('posts_where', function($where, $query) {
  global $wpdb;

  // Check if 'post_type' exists in the query and is an array
  if ( isset($query->query['post_type'] ) && is_array( $query->query['post_type'] )) {
    
    // Check if the query includes "attachment" in the post type argument
    if ( in_array( 'attachment', $query->query['post_type'] ) ) {
      $where .= ' AND ' . $wpdb->posts . '.post_mime_type NOT LIKE \'image/%\'';
    }
  }
  return $where;
}, 10, 2);
2 months ago
<?php
// output only sub categories of current woocommerce product category
add_filter( 'facetwp_facet_render_args', function( $args ) {

    if ( 'my_facet_name' == $args['facet']['name'] && is_product_category() ) {  // replace 'my_facet_name' with the name of your facet

        $current_term = get_queried_object_id();

        foreach ( $args['values'] as $key => $row ) {
            /* to also include current term in chocies add this to if:
               && $row['term_id'] != $current_term
            */
            if ( $row['parent_id'] != $current_term ) {
                unset( $args['values'][$key] );
            }
        }

        $args['values'] = array_values( $args['values'] ); // fixes indexes
    }

    return $args;
});