Created
March 31, 2016 17:50
-
-
Save wunderdojo/e0d5ef75dc76609f32de9eabc324306a to your computer and use it in GitHub Desktop.
ACF 5 Custom Location Rule using Taxonomy Name and Slug
This file contains 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 | |
//-http://www.advancedcustomfields.com/resources/custom-location-rules/ | |
//- Create new rule type in the Location rules section | |
add_filter( 'acf/location/rule_types', 'acf_location_rules_types' ); | |
function acf_location_rules_types( $choices ){ | |
$choices['Post']['taxonomy_slug'] = 'Fee Type'; | |
return $choices; | |
} | |
//- Populate the values for the new rule type | |
add_filter( 'acf/location/rule_values/taxonomy_slug', 'acf_location_rules_values_taxonomy_slug' ); | |
function acf_location_rules_values_taxonomy_slug( $choices ){ | |
$fee_types = get_terms( 'fees', array( 'hide_empty'=>false ) ); | |
if( $fee_types ){ | |
foreach ( $fee_types as $type ) { | |
$choices[$type->slug] = $type->name; | |
} | |
} | |
return $choices; | |
} | |
//- Apply the rule filters when displaying a product edit screen | |
add_filter( 'acf/location/rule_match/taxonomy_slug', 'acf_location_rules_match_taxonomy_slug', 10, 3 ); | |
function acf_location_rules_match_taxonomy_slug( $match, $rule, $options ){ | |
//- validate | |
if( !$options['post_id'] ){ | |
return false; | |
} | |
//- post type | |
if( !$options['post_type'] ) { | |
$options['post_type'] = get_post_type( $options['post_id'] ); | |
} | |
//- vars | |
$taxonomies = get_object_taxonomies( $options['post_type'] ); | |
$terms = $options['post_taxonomy']; | |
//- not AJAX -- we're loading the post initially | |
if( ! $options['ajax'] ) { | |
//- no terms? Load them from the post_id | |
if( empty( $terms ) ) { | |
if( is_array( $taxonomies ) ) { | |
foreach( $taxonomies as $tax ) { | |
$all_terms = get_the_terms( $options['post_id'], $tax ); | |
if( $all_terms ) { | |
foreach( $all_terms as $all_term ) { | |
$terms[] = $all_term->term_id; | |
} | |
} | |
} | |
} | |
} | |
//- no terms at all? | |
if( empty( $terms ) ){ | |
//- If no terms, this is a new post and should be treated as if it has the "Uncategorized" (1) category ticked | |
if( is_array( $taxonomies ) && in_array( 'category', $taxonomies ) ) { | |
$terms[] = '1'; | |
} | |
} | |
}//-end of non-ajax rules application | |
//- each product should only have one fee category but ACF returns an array with the first value empty | |
//- Ex: array( 0 => , 1 => 8 ); | |
$match_term = array_pop( $terms ); | |
$term = get_term_by( 'id', $match_term, 'fees' ); | |
if( $rule['operator'] == "==" ) { | |
$match = false; | |
if( $term ){ | |
if( $rule['value'] == $term->slug ){ | |
$match = true; | |
} | |
} | |
} | |
elseif( $rule['operator'] == "!=" ) { | |
$match = true; | |
if( $term ) { | |
if( $rule['value'] != $term->slug ) { | |
$match = true; | |
} | |
} | |
} | |
return $match; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment