Created
October 12, 2016 01:53
-
-
Save machouinard/3734f0da868ae9cc7329426662e8034d to your computer and use it in GitHub Desktop.
Custom ACF location rule for The Events Calendar Event category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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