Skip to content

Instantly share code, notes, and snippets.

@gzalinski
Last active May 25, 2022 14:35
Show Gist options
  • Save gzalinski/4b1e774e47117d132c8777057c743883 to your computer and use it in GitHub Desktop.
Save gzalinski/4b1e774e47117d132c8777057c743883 to your computer and use it in GitHub Desktop.
[ACF rule terms] ACF specific taxonomy term #wp #acf
<?php
add_filter( 'acf/location/rule_types', 'acf_location_rules_types', 999 );
function acf_location_rules_types( $choices ) {
$tax_label = __( 'News Type' ); //<--YOUR LABEL
$key = __('Forms', 'acf');
if ( ! isset( $choices[ $key ] ) ) {
$choices[ $key ] = [];
}
$choices[ $key ]['category_id'] = $tax_label;
return $choices;
}
add_filter( 'acf/location/rule_values/category_id', 'acf_location_rules_values_category' );
function acf_location_rules_values_category( $choices ) {
$tax_id = 'news_type'; //<-- YOUR TAX ID
$terms = get_terms( $tax_id, [ 'hide_empty' => false ] );
if ( $terms && is_array( $terms ) ) {
foreach ( $terms as $term ) {
$choices[ $term->term_id ] = $term->name;
}
}
return $choices;
}
add_filter( 'acf/location/rule_match/category_id', 'acf_location_rules_match_category', 10, 3 );
function acf_location_rules_match_category( $match, $rule, $options ) {
$tax_id = 'news_type'; //<-- YOUR TAX ID
$screen = get_current_screen();
if ( $screen->base !== 'term' || $screen->id !== 'edit-'.$tax_id ) {
return $match;
}
$term_id = $_GET['tag_ID'];
$selected_term = $rule['value'];
if ( $rule['operator'] == '==' ) {
$match = ( $selected_term == $term_id );
} elseif ( $rule['operator'] == '!=' ) {
$match = ( $selected_term != $term_id );
}
return $match;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment