0
votes

I am using the Advanced Custom Fields plugin on a WordPress site. I am aiming to have a custom location rule to show the custom filed group when any term within a custom taxonomy is selected.

Following the custom location rule tutorial on the ACF site I have added custom types, operators, and values to the rules row.

My matching function (code below) works only when the page is reloaded, but not via AJAX. How do I add custom taxonomies to the $options array so the matching function can evaluate via AJAX when custom taxonomy terms are checked/unchecked.

function acf_location_rules_match_taxonomyTerm( $match, $rule, $options ){
    // vars
    $taxonomies = get_object_taxonomies( $options['post_type'] );
    $terms = $options['taxonomy'];
    // not AJAX
    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;
                        }
                    }
                }
            }
        }
        if($rule['operator'] == "<==>"){
            $match = false;
            if($terms){
                $current_terms = get_the_terms($options['post_id'], $rule['value']);
                if ( $current_terms && ! is_wp_error( $terms ) ) {
                    $current_term_ids = array();
                    foreach ($current_terms as $current_term) {
                        $current_term_ids[] = $current_term->term_id;
                    }
                }
                foreach ($current_term_ids as $current_term_id) {
                    if( in_array($current_term_id, $terms) ){
                        $match = true;
                    }
                }
            }
        }
        else{
            $match = false;
        }
    }
    return $match;
}
1

1 Answers

1
votes

Have you tried this using this plugin? might help. though its a bit late. :)

https://wordpress.org/plugins/advanced-custom-fields-meta-location-rule/