Gists

5 days 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 });
6 days 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
});
6 days 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 weeks 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 weeks 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);
4 weeks 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;
});
1 month 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 );
1 month ago
<?php
/**
 ** including a facetwp shortcode in a block pattern
 **/
if ( ! is_admin() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
    echo do_shortcode('[facetwp facet="categories"]');
} else {    
    echo '<!-- wp:shortcode -->[facetwp facet="categories"]<!-- /wp:shortcode -->';
} ?>
2 months ago
<?php
/** fix incorrect post_ok=false in relevanssi  **/
add_filter( 'facetwp_facet_filter_posts', function( $return ) {
    // remove filter before running relevanssi search
    remove_filter( 'post_password_required', 'rcp_post_password_required_rest_api', 10 );
    return $return;
}, 1 );
2 months ago
<?php
// If you are using the Submit Button add-on and want the redirect not to happen when your facet is empty, 
// use the following script and de-activate the add-on. This is basically the same as facetwp-submit.js, but with an 
// extra check in L13-14 to check if a specific facet is empty when the Submit button is clicked. If it is, the redirect does not happen.
// This can be useful to prevent 'empty' redirects for facet types that refresh themselves, like an Autocomplete facet that submits on Enter or clicking a selection.

add_action( 'facetwp_scripts', function() {
  ?>
  <script>
    (function($) {
      $().on('click', '.fwp-submit', function() {
        FWP.parseFacets();
        
        // Do nothing if this facet is empty
        if ( ! FWP.facets['my_facet_name'].length ) { // Replace 'my_facet_name' with the name of your facet
          return;
        }

        var href = $(this).attr('data-href');
        var query_string = FWP.buildQueryString();

        if (query_string.length) {
          var prefix = (-1 < href.indexOf('?')) ? '&' : '?';
          href += prefix + query_string;
        }

        window.location.href = href;
      });
    })(fUtil);
  </script>
  <?php
}, 100 );