Skip to content

Instantly share code, notes, and snippets.

@martijngastkemper
Last active October 30, 2016 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martijngastkemper/350bda966be1d4e411db01a561ef89e5 to your computer and use it in GitHub Desktop.
Save martijngastkemper/350bda966be1d4e411db01a561ef89e5 to your computer and use it in GitHub Desktop.
Create a custom ACF rule to match all pages having a taxonomy enabled.
<?php
// functions.php
add_filter('acf/location/rule_types', 'acf_location_rules_types');
function acf_location_rules_types( $choices ) {
$choices['Post']['post-type-has-taxonomy'] = __('Post Type has Taxonomy');
return $choices;
}
add_filter( 'acf/location/rule_values/post-type-has-taxonomy', 'acf_location_rules_values_has_taxonomy' );
function acf_location_rules_values_has_taxonomy( $choices ) {
return array_merge($choices, get_taxonomies());
}
add_filter('acf/location/rule_match/post-type-has-taxonomy', 'acf_location_rules_match_has_taxonomy', 10, 3);
function acf_location_rules_match_has_taxonomy( $match, $rule, $options )
{
if ($rule['operator'] == '==') {
$match = is_object_in_taxonomy($options['post_type'], $rule['value']);
} elseif ($rule['operator'] == '!=') {
$match = !is_object_in_taxonomy($options['post_type'], $rule['value']);
}
return $match;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment