Support

Account

Home Forums General Issues Custom rule for any term within a taxonomy Reply To: Custom rule for any term within a taxonomy

  • This is my matching function. It functions correctly but only on page load. Is anyone able to suggest how to get custom taxonomy terms into the $options array so the function can evaluated on changes to the custom taxonomy.

    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;
    }