Gists

4 days ago
<?php
// Adds FacetWP compatibility to the Avada theme's Blog element.
// For this to work, the Blog listing also needs a 'facetwp-template' class.

add_filter( 'fusion_blog_shortcode_query_args', function( $query_args ) {  
  $query_args['facetwp'] = true;
  return $query_args;
}, 10 );
4 days ago
<?php
add_filter( 'facetwp_query_args', function( $query_args, $class ) {
  if ( 'portfolio' == $class->ajax_params['template'] ) { // Change 'portfolio' to the name of your Listing Builder listing
    $query_args['ignore_sticky_posts'] = true; 
  }
  return $query_args;
}, 10, 2);
1 week ago
<?php
// Add an aria-live=”polite” attribute to the <div> with class "facetwp-template", generated by a Listing Builder listing template.

add_filter( 'facetwp_shortcode_html', function( $output, $atts ) {
  if ( isset( $atts['template'] ) ) {
    $output = str_replace( 'class="facetwp-template"', 'class="facetwp-template" aria-live="polite"', $output );
  }
  return $output;
}, 10, 2 );
2 weeks ago
<?php
add_filter('facetwp_asset_html', function($html, $url) {
    
  // Check if it's a script tag
  if (strpos($html, '<script') !== false) {
    
      // Add the data-cookieconsent attribute
    $html = str_replace('<script ', '<script data-cookieconsent="ignore" ', $html);
  }
    
  return $html;
}, 10, 2);
2 weeks ago
<?php
// Schedules a one-time event to programmatically re-index all posts.
// See this section for how to programmatically schedule one-time re-indexing of all, or a selection of posts:
// https://facetwp.com/help-center/indexing/#trigger-one-time-re-indexing-with-wp-cron
// For periodic, recurring re-indexing, see our Schedule Indexer add-on:
// https://facetwp.com/help-center/add-on-features-and-extras/schedule-indexer/
// To do this for a single post ID, see: https://gist.facetwp.com/gist/facetwp-schedule-a-one-time-wp-cron-event-to-re-index-a-single-post/

// Needs to be availble for WP Cron to run 
function fwp_all_index() {
    FWP()->indexer->index();
}
add_action( 'fwp_all_index', 'fwp_all_index' );

// Place this in a hook or function where you want to initiate indexing, for example after finishing an import. 
wp_schedule_single_event( time(), 'fwp_all_index';
3 weeks ago
<?php
add_action( 'facetwp_scripts', function() {
  ?>
    <script>
      document.addEventListener('facetwp-loaded', function() {
          if ( FWP.loaded ) {
              jQuery('.wp-audio-shortcode').mediaelementplayer();
          }
      });
    </script>
  <?php
}, 100 );
4 weeks ago
<?php
// When there are no results, Bricks does not output the facetwp-template class, leading to empty facets

// Edit the page and open the query settings for the element where Use FacetWP is enabled. 
// Scroll to the bottom of the query settings and add a text (not template) to display for No Results
// Then add the following snippet to your (child) theme's function.php
// Replace the id with the id of your Bricks element

add_filter( 'bricks/query/no_results_content', function( $content, $settings, $element_id ) {

	if ( true == ( $settings["usingFacetWP"] ?? false ) ) {
		$content = '<div id="brxe-olglkr" class="brxe-div facetwp-template">' . $content . '</div>';
	}
	return $content;

}, 10, 3 );
4 weeks ago
<?php
// Order by a custom field, but prevent posts from disappearing when that field does not exist for those posts.
// It first sorts by the reviews_rating field, then by post title.
// Posts without reviews_rating are sorted by title and added after the ones with the reviews_rating. 

// Important:
// - NOT EXISTS needs to be before EXISTS
// - In 'orderby' it needs to use 'meta_value_num' or 'meta_value' instead of a named meta_query segment.
// - Use 'meta_value' if the custon field is not numerical
// - The order needs to be DESC for the posts without the rating to appear after the ones with it
// See: https://wordpress.stackexchange.com/a/329687 

// Related: 
// https://gist.facetwp.com/gist/gist-bcd7971b433440a5a73b99c73b530a0f/
// https://facetwp.com/how-to-filter-or-sort-a-wp_query-by-one-or-more-custom-fields/#order-a-query-by-a-custom-field
// https://facetwp.com/how-to-filter-or-sort-a-wp_query-by-one-or-more-custom-fields/#order-a-query-by-multiple-custom-fields-that-can-be-empty-for-some-posts
// https://facetwp.com/help-center/developers/hooks/output-hooks/facetwp_facet_sort_options/#add-an-option-to-sort-by-a-custom-field-and-include-posts-without-that-field

add_filter('facetwp_facet_sort_options', function ($options, $params) {

  $options['rating'] = [

    'label' => 'Rating',
    'query_args' => [
      'meta_query' => [
        'relation' => 'OR',
        [
          'key' => 'reviews_rating',
          'compare' => 'NOT EXISTS'
        ],
        [
          'key' => 'reviews_rating',
          'compare' => 'EXISTS'
        ]
      ],
      'orderby' => [
        'meta_value_num' => 'DESC', // Order by rating first. Needs to be 'DESC'. Note: use 'meta_value' if the custom field is not numerical
        'title' => 'ASC' // Order by title second
      ],
      'meta_key' => 'reviews_rating'
    ]
  ];

  return $options;

}, 10, 2 );
4 weeks ago
<?php
/**
 ** Facet automatically indexes latitude and longitude from an ACF map field
 ** This will index other data saved in other sub fields
 ** Note that these fields are optional and may not exist
 ** See ACF docs for more - https://www.advancedcustomfields.com/resources/google-map/#template-usage
 **/

add_filter( 'facetwp_index_row', function( $params, $class ) {
	if ( 'my_facet' == $params['facet_name'] ) { // change my_facet to name of your facet
		$post_id = $params['post_id'];
		$acf = str_replace( 'acf/', '', $params['facet_source'] );
		$location = get_field( $acf, $post_id );
		$value = $location['city'] ?? ''; 
		if ( '' != $value ) {
			$params['facet_value'] = $value;
			$params['facet_display_value'] = $value;
		} else {
			$params['facet_value'] = ''; // don't index
		}
	}
	return $params;
}, 10, 2 );
1 month ago
<?php
// Adds wrappers with class "column-wrapper" surrounding top-level 'facetwp-checkbox' elements and their following sibling 'facetwp-depth' elements, for column styling
// Replace 'myfacetname' with the name of your facet

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

      // Get all top-level facetwp-checkbox elements
      var checkboxes = document.querySelectorAll('.facetwp-facet-myfacetname > .facetwp-checkbox');

      // Loop through each checkbox
      checkboxes.forEach(function(checkbox) {
          
        // Create a new div element for the wrapper
        var wrapper = document.createElement('div');
        wrapper.classList.add('column-wrapper');

        // Find the next sibling div with class facetwp-depth
        var depth = checkbox.nextElementSibling;

        // Append the wrapper before the current checkbox
        checkbox.parentNode.insertBefore(wrapper, checkbox);

        // Append the checkbox element to the wrapper
        wrapper.appendChild(checkbox);

        // Check if the depth element exists and has the class facetwp-depth
        if (depth && depth.classList.contains('facetwp-depth')) {

          // Append the depth element to the wrapper
          wrapper.appendChild(depth);
        }
      });
    });
  </script>
  <?php
}, 100 );