Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active December 20, 2017 08:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliffordp/eb2753491526b079ba69882f4be4fafc to your computer and use it in GitHub Desktop.
Save cliffordp/eb2753491526b079ba69882f4be4fafc to your computer and use it in GitHub Desktop.
The Events Calendar - Filter Bar -- make Event Categories filter by AND (in all selected categories) instead of OR (in any selected category, which is the default)
<?php
/**
* !!!
* Old snippet. Use the tribe_events_filter_taxonomy_relationship filter instead.
* https://gist.github.com/cliffordp/c2ea6115318044db219322fea8da55ec
* !!!
*
* By Barry 2016-07-06
* From https://gist.github.com/cliffordp/eb2753491526b079ba69882f4be4fafc
*
* See https://gist.github.com/niconerd/e5b624a9e85e0895ff4393354e4d2ef0 for similar solution but for Tags
*
* Attempts to modify the taxonomy query added by Filter Bar's
* category filter to use AND type logic instead of OR.
*
* For example, by default, if Categories A and B are selected
* using Filter Bar it will return events in EITHER of those
* categories. With this modification in place, however, it
* will whittle things down to only events that are assigned
* to BOTH those terms.
*
* Takes a slightly shotgun approach, would need refinement in
* anything beyond very simple scenarios.
*/
class Category_Filter__Use_And_Logic {
private static $engage = false;
static function init() {
add_action( 'tribe_events_filter_taxonomy_relationship', __CLASS__ . '::listen' );
add_action( 'tribe_events_pre_get_posts', __CLASS__ . '::change', 100 );
}
static function listen() {
self::$engage = true;
}
static function change( $query ) {
if ( ! self::$engage ) return;
$tax_query = $query->get( 'tax_query' );
if ( ! is_array( $tax_query ) || ! isset( $tax_query[ 'Tribe__Events__Filterbar__Filters__Category' ] ) ) return;
foreach ( $tax_query[ 'Tribe__Events__Filterbar__Filters__Category' ] as &$inner_query )
if ( is_array( $inner_query ) ) $inner_query['operator'] = 'AND';
$query->set( 'tax_query', $tax_query );
}
}
/* Enable AND type logic for category filters.
*
* Comment out or delete the following line
* to temporarily disable this mod.
*/
Category_Filter__Use_And_Logic::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment