Gists

1 week 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);
4 weeks 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 );
4 weeks 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;
1 month 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 });
1 month 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
});
1 month 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;
});
3 months ago
<?php

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

    if ( 'my_facet_name' ==  $params[ 'facet_name' ] ) { // replace 'my_facet_name' with name of facet to index with hierarchy

        if ( 0 === strpos( $params['facet_source'], 'cf/attribute_pa_' ) ) {
            $taxonomy = str_replace( 'cf/attribute_', '', $params['facet_source'] );
            $term = get_term_by( 'slug', $params['facet_value'], $taxonomy );

            if ( false !== $term ) {
                $params['term_id'] = $term->term_id;
                $params['parent_id'] = $term->parent;
                $params['facet_display_value'] = $term->name;
                $params['depth'] = count( get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ) );
            }
        }

    } 

    return $params;

}, 10, 2 );