Skip to content

Instantly share code, notes, and snippets.

@machouinard
Created October 12, 2016 01:53
Show Gist options
  • Save machouinard/3734f0da868ae9cc7329426662e8034d to your computer and use it in GitHub Desktop.
Save machouinard/3734f0da868ae9cc7329426662e8034d to your computer and use it in GitHub Desktop.
Custom ACF location rule for The Events Calendar Event category
<?php
//* Add custom location rule type to column one for selecting an Event category
add_filter( 'acf/location/rule_types', 'mc_location_rule_types' );
function mc_location_rule_types( $choices ) {
$choices['Events']['event_type'] = 'Event Type';
return $choices;
}
//* Add custom location rule value to column three - loops through each Event category and adds its name as a choice
add_filter( 'acf/location/rule_values/event_type', 'mc_location_rule_values_event_cat' );
function mc_location_rule_values_event_cat( $choices ) {
$cats = get_terms( 'tribe_events_cat', array( 'hide_empty' => 0 ) );
if ( ! is_wp_error( $cats ) ) {
foreach ( $cats as $cat ) {
//* Set term ID as key for comparison when matching
$choices[ $cat->term_id ] = $cat->name;
}
}
return $choices;
}
//* Add custom match element for the custom location rule value
add_filter( 'acf/location/rule_match/event_type', 'mc_location_rule_match_event_cat', 10, 3 );
function mc_location_rule_match_event_cat( $match, $rule, $options ) {
//* Only check Events
if ( function_exists( 'tribe_is_event' ) && tribe_is_event( get_the_ID() ) ) {
//* Get all Event categories for current post
$event_cats = wp_get_post_terms( get_the_ID(), 'tribe_events_cat', array( 'fields' => 'ids' ) );
$selected_cat = (int) $rule['value'];
if ( $rule['operator'] == "==" ) {
$match = ( in_array( $selected_cat, $event_cats ) );
} elseif ( $rule['operator'] == "!=" ) {
$match = ( ! in_array( $selected_cat, $event_cats ) );
}
}
return $match;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment