Support

Account

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

Solving

Custom rule for any term within a taxonomy

  • Is it possible to create a custom location rule for any term selected within a custom taxonomy?

    I can achieve this with a long list of ‘AND’ rules for each term, but this introduces the need to add a new rule for each term added to the taxonomy.

  • Have you tried to set the location rule to Taxonomy Term and select your custom taxonomy?

    This will add your field to all your terms in selected taxonomy.

  • Hey Fredrik. Yes I tried setting the location rule to Taxonomy Term but that makes the fields related to the taxonomy and visible on the “edit taxonomy terms” page.

    I’m aiming to have my custom fields related to posts and visible on the post edit page when a term from my custom taxonomy is selected.

  • I have added custom types, operators and values to the location rule row by following the tutorial.

    In my matching rule I’m unable to see any custom taxonomies in the $options array.

    Do I have to do anything to add custom taxonomies to the $options array?

  • 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;
    }
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Custom rule for any term within a taxonomy’ is closed to new replies.