Gists

2 weeks ago
<?php
/**
 * See: https://facetwp.com/help-center/developers/hooks/advanced-hooks/facetwp_facet_sources/#remove-custom-fields
 * Removes all data sources that are custom fields with "_" named meta_key from data sources in FacetWP settings
 * Keys in sources include "custom_fields", "posts, "taxonomies"
 * Integrations add their own sources: e.g. "upt/", "woocommerce/", "acf"
 * Context is 'default' for facet settings, context would be 'builder' for Listing Builder items
 **/
add_filter( 'facetwp_facet_sources', function( $sources, $context ) {
    foreach ( $sources AS $key => $value ) {
        if ( "custom_fields" == $key && 'default' == $context ) {
            $choices = array_values( array_filter( $value['choices'], function( $value, $key ) {
                if ( str_starts_with( $key, "cf/_" ) ) {
                    return false;
                }
                return true;
            }, ARRAY_FILTER_USE_BOTH ) );
            $sources[$key]['choices'] = $choices;
        }
    }
    return $sources;
}, 10, 2 );
2 weeks ago
<?php
// See: https://facetwp.com/help-center/developers/hooks/advanced-hooks/facetwp_facet_sources/#add-a-non-existing-custom-field-option-and-handle-its-indexing

// Add the new data source
add_filter( 'facetwp_facet_sources', function( $sources, $context ) {
  if ( 'default' == $context ) { // Only in facets' Data source dropdowns, not in Listing Builder item dropdowns
    $sources['woocommerce']['choices']['woo/price_incl_tax'] = __( 'Price (incl. tax)' );
  }
  return $sources;
}, 20, 2 );



// Handle the new data source
add_filter( 'facetwp_indexer_post_facet', function( $return, $params ) {
    $facet = $params['facet'];
    $defaults = $params['defaults'];
    $post_id = (int) $defaults['post_id'];

    if ( 'product' == get_post_type( $post_id ) && 'woo/price_incl_tax' == $defaults['facet_source'] ) {
        $product = wc_get_product( $post_id );
        $price = wc_get_price_including_tax( $product );
        $defaults['facet_value'] = $price;
        $defaults['facet_display_value'] = $price;
        FWP()->indexer->index_row( $defaults );
        return true;
    }

    return $return;
}, 10, 2 );
2 weeks ago
<?php
// See: https://facetwp.com/help-center/developers/hooks/advanced-hooks/facetwp_facet_sources/#remove-custom-fields

add_filter( 'facetwp_facet_sources', function( $sources ) {
    foreach ( $sources['custom_fields']['choices'] as $key => $value ) {
        if ( 0 === strpos( $value, '_stcr' ) ) {
            unset( $sources['custom_fields']['choices'][ $key ] );
        }
    }
    return $sources;
});
2 weeks ago
<?php
// See: https://facetwp.com/how-to-filter-wp-attachments-and-draft-pending-or-private-posts/#create-a-media-type-facet

// Index attachements: 'inherit' is needed for attachment indexing.
add_filter( 'facetwp_indexer_query_args', function( $args ) {
  $args['post_status'] = array( 'publish', 'inherit' );
  return $args;
});

// Add a "Media Type" option to the facet and Listing Builder data sources dropdown
add_filter( 'facetwp_facet_sources', function( $sources, $context ) {
  $sources['posts']['choices']['post_mime_type'] = 'Media Type';
  return $sources;
}, 20, 2 );
 
// Modify the indexed value for the post_mime_type. 
// Adapt as needed.
add_filter( 'facetwp_index_row', function( $params, $class ) {
  if ( 'post_mime_type' == $params['facet_source'] ) {
    
    $value = $params['facet_display_value'];
 
    $parts = explode( '/', $value ); // The full post_mime_type, e.g. 'image/png'
    $type    = $parts[0]; // e.g., 'image'
    $subtype = $parts[1]; // e.g., 'png'
    
    $params['facet_value'] = $params['facet_display_value'] = $subtype; // Index the subtype, e.g. 'png'
 
  }
  return $params;
}, 10, 2 );