Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
mgibbs189 / functions.php
Created October 27, 2020 15:06
FacetWP - checkbox facet to show posts with images
<?php
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'has_image' == $params['facet_name'] ) {
$post_id = (int) $params['post_id'];
if ( has_post_thumbnail( $post_id ) ) {
$params['facet_value'] = 1;
$params['facet_display_value'] = 'Has photos';
}
@mgibbs189
mgibbs189 / test.js
Created October 23, 2020 14:10
FacetWP - ignore URL hash except when resetting
<script>
(function($) {
$(document).on('facetwp-refresh', function() {
if (! FWP.loaded) {
FWP.temp_set_hash = FWP.set_hash; // on init, save a backup of FWP.set_hash()
}
if (FWP.is_reset) {
FWP.set_hash = FWP.temp_set_hash; // on reset, store FWP.set_hash()
}
else {
@mgibbs189
mgibbs189 / functions.php
Last active February 12, 2021 12:44
FacetWP - translate "On sale" text
<?php
add_filter( 'gettext', function( $translated_text, $text, $domain ) {
if ( 'fwp-front' == $domain && 'Go' == $text ) {
$translated_text = 'Go (russian)';
}
return $translated_text;
}, 10, 3 );
@mgibbs189
mgibbs189 / functions.php
Last active October 19, 2020 14:34
FacetWP - ignore WC REST API queries
<?php
// Add to your (child) theme's functions.php
// woocommerce_rest_{$this->post_type}_object_query
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
if ( false !== strpos( $_SERVER['REQUEST_URI'], 'wp-json/wc/v3' ) ) {
$is_main_query = false;
}
return $is_main_query;
@mgibbs189
mgibbs189 / functions.php
Created October 9, 2020 15:10
FacetWP - ignore any query using "showposts"
<?php
// Add to your (child) theme's functions.php
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
if ( false !== $query->get( 'showposts', false ) ) {
$is_main_query = false;
}
return $is_main_query;
}, 10, 2 );
@mgibbs189
mgibbs189 / functions.php
Last active October 6, 2020 01:09
FacetWP - sort by menu_order for certain post type
<?php
// Add to your (child) theme's functions.php
add_action( 'pre_get_posts', function( $query ) {
if ( 'providers' == $query->get( 'post_type' ) ) {
$query->set( 'orderby', [ 'menu_order' => 'ASC', 'date' => 'DESC' ] );
}
});
@mgibbs189
mgibbs189 / functions.php
Created September 16, 2020 14:48
Ignore certain post type archives
<?php
// Add to your (child) theme's functions.php
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
if ( $query->is_post_type_archive( [ 'opleidingen', 'cursussen', 'groepsleertherapie' ] ) ) {
$is_main_query = false;
}
return $is_main_query;
}, 10, 2 );
@mgibbs189
mgibbs189 / functions.php
Last active September 15, 2020 13:13
Listable - exclude query for listing archives
<?php
// Add to your (child) theme's functions.php
add_filter( 'facetwp_is_main_query', function( $is_main_query, $query ) {
if ( $query->is_tax( 'job_listing_category' ) ) {
$is_main_query = false;
}
return $is_main_query;
}, 10, 2 );
@mgibbs189
mgibbs189 / functions.php
Created September 1, 2020 12:53
FacetWP - force no results when proximity is empty
<?php
// Add to your (child) theme's functions.php
add_filter( 'facetwp_pre_filtered_post_ids', function( $post_ids, $class ) {
$facet_name = 'senior_living_proximity';
if ( empty( $class->facets[ $facet_name ]['selected_values'] ) ) {
$post_ids = [ 0 ]; // force a 0-filled (empty) array
}
@mgibbs189
mgibbs189 / functions.php
Last active August 21, 2020 13:07
FacetWP - layout builder - hide title if an excerpt exists
<?php
// Add to your (child) theme's functions.php
add_filter( 'facetwp_builder_item_value', function( $value, $item ) {
if ( 'post_title' == $item['source'] ) {
$excerpt = get_the_excerpt( $GLOBALS['post']->ID );
if ( ! empty( $excerpt ) ) {
$value = ''; // clear the layout builder item value