Gists

2 weeks ago
<?php
/** use this snippet with facetwp-template set as a custom class on the 
 ** query loop block for facet compatiblity
 **/

add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'facetwp-template' ) !== false ) {
        $query_args['facetwp'] = true;
    }
    return $query_args;
}, 10, 2 );
2 weeks ago
<?php
add_action( 'wp_footer', function() {
  ?>
    <script>
      document.addEventListener('facetwp-loaded', function() {
        EleCustomSkinItemLink();
      });
    </script>
  <?php
}, 100 );
3 weeks ago
<?php

// Add to your (child) theme's functions.php and re-index afterwards

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'audience' == $params['facet_name'] ) {
        if ( 'for-all' == $params['facet_value'] ) {
            $new_row = $params;

            $new_row['facet_value'] = 'for-individual-employees';
            $new_row['facet_display_value'] = 'For Individual Employees';
            $class->insert( $new_row );

            $new_row['facet_value'] = 'for-jobseekers';
            $new_row['facet_display_value'] = 'For Jobseekers';
            $class->insert( $new_row );

            $new_row['facet_value'] = 'for-hr-professionals-team-leaders-anddi-practicion';
            $new_row['facet_display_value'] = 'HR Professionals, Team Leaders and D&I Practitioners';
            $class->insert( $new_row );
        }
    }
    return $params;
}, 10, 2 );
3 weeks ago
<?php
// Each facet's source now can be accessed in JavaScript with FWP.settings.facet_sources['your_facet_name']

add_filter( 'facetwp_render_output', function( $output, $params ) {
  $sources = [];

  foreach ( FWP()->facet->facets as $facet_name => $facet_data ) {
    if ( isset ( $facet_data['source'] ) ) {
      $sources[ $facet_name ] = $facet_data['source'];
    }
  }

  $output['settings']['facet_sources'] = $sources;
  return $output;
}, 10, 2 );
1 month ago
<?php

// Replace "my_dropdown_facet_name" with the name of your Dropdown facet
// And replace "my_option" with the value of the option that you want to be hidden
// The "disabled" attribute is added for older browsers that do not support "hidden"

add_action( 'wp_head', function() {
  ?>
    <script>
      (function($) {
        $(document).on('facetwp-loaded', function() {
         $('.facetwp-facet-my_dropdown_facet_name option[value="my_option"]').prop("hidden", true).prop("disabled", true);
      });
      })(jQuery);
    </script>
  <?php
}, 100 );
1 month ago
// Init and reload masonry layout after facet filtering
// Works for https://masonry.desandro.com
// This needs the imagesLoaded() script, to be downloaded separately: 
// https://masonry.desandro.com/layout.html#imagesloaded

// Init Masonry
var $grid = jQuery('.facetwp-template').masonry({
  itemSelector: '.facetwp-template',
  columnWidth: 220,
  gutter:20,
});

// Reload and update on facetwp-loaded
jQuery(document).on('facetwp-loaded', function() {
  $grid.masonry('reloadItems')

  // Update Masonry layout after each image has loaded
  $grid.imagesLoaded().progress( function() {
    $grid.masonry('layout');
  });
});
1 month ago
<?php

// Retrigger/reload masonry layout after facet filtering
// Works for https://masonry.desandro.com

// If you get overlapping images etc. use the imagesLoaded check like this:
// https://gist.facetwp.com/gist/reload-masonry-layout-after-filtering-with-imagesloaded/

add_action( 'wp_head', function() { ?>

    <script>
      (function($) {
        $(document).on('facetwp-loaded', function() {
          if (FWP.loaded) {
            $('.facetwp-template').masonry('reloadItems').masonry('layout');
          }
        });
      })(jQuery);
    </script>

<?php }, 100 );
1 month ago
// Order a query by an post type
// You can just use the 'orderby' query argument to order by 'type' (= post type) to order a query by post type, 
// but only alphabetically, with the order set to ASC or DESC. 
// If you need an arbitrary order, you can use the following:

function order_search_by_posttype( $orderby, $wp_query ) {

  global $wpdb;

  // filter the correct query by using the original post_type argument as check
  if ( $wp_query->query_vars['post_type'] == [
      'portfolio',
      'product',
      'job_listing'
    ] ) {
      
    // set the new order of the post types
    $orderby =
      "
            CASE WHEN {$wpdb->prefix}posts.post_type = 'job_listing' THEN '1'
                 WHEN {$wpdb->prefix}posts.post_type = 'product' THEN '2' 
                 WHEN {$wpdb->prefix}posts.post_type = 'portfolio' THEN '3' 
            ELSE {$wpdb->prefix}posts.post_type END ASC, 
            {$wpdb->prefix}posts.post_title ASC";
  }

  return $orderby;
}

add_filter( 'posts_orderby', 'order_search_by_posttype', 10, 2 );
2 months ago
// For facets with post types as source, the name that is shown in the facet's choices is the labels->name set when registering the post type.
// See: https://developer.wordpress.org/reference/functions/register_post_type/
// If you need it to be something else, e.g. labels->singular_name, the following code will index that name.
// Replace 'my_facet_name' with the name of your facet.
// Post type labels array overview: https://developer.wordpress.org/reference/functions/get_post_type_labels/

add_filter( 'facetwp_index_row', function( $params, $class ) {
  $name = $params['facet_name'];
  if ( in_array( $name, [ 'my_facet_name' ] ) ) { // you can add multiple facets to the array
    $post_id = (int) $params['post_id'];
    $post_type_obj = get_post_type_object( get_post_type($post_id) );
    $params['facet_display_value'] = $post_type_obj->labels->singular_name;
  }
  return $params;
}, 10, 2 );
2 months ago
<?php
/** change to ignore_sticky_posts when filtering **/

add_filter( 'pre_get_posts', function( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) { // is this the blog home page query?
        $filtered = ( FWP()->request->is_preload && !empty( FWP()->request->url_vars ) ) ||
        ( FWP()->request->is_refresh && !empty( $_POST['data']['http_params']['get'] ) ) ? true : false;
        if ( $filtered ) {
            $query->set( 'ignore_sticky_posts', true );
        }
    }
    return $query;
}, 998, 2 );