Support

Account

Home Forums Feature Requests Show field when taxonomy type is enabled

Solved

Show field when taxonomy type is enabled

  • I would like to show a field for post types which have a certain taxonomy enabled. Currently you have to specify a specific taxonomy entry. It’s like the Post Type, but for enabled taxonomies.

  • There is no built in rule for this and doing a search I didn’t come up with any solutions. This would need a custom location rule “Post type has taxonomy” or something like that. https://www.advancedcustomfields.com/resources/custom-location-rules/

  • Thanks for pointing me in the right direction. Very simple solution.

    Having suggestion to enhance this peace of code fork the Gist.

    add_filter('acf/location/rule_types', 'acf_location_rules_types');
    function acf_location_rules_types( $choices ) {
        $choices['Post']['post-type-has-taxonomy'] = __('Post Type has Taxonomy');
        return $choices;
    }
    
    add_filter( 'acf/location/rule_values/post-type-has-taxonomy', 'acf_location_rules_values_has_taxonomy' );
    function acf_location_rules_values_has_taxonomy( $choices ) {
        return array_merge($choices, get_taxonomies());
    }
    
    add_filter('acf/location/rule_match/post-type-has-taxonomy', 'acf_location_rules_match_has_taxonomy', 10, 3);
    function acf_location_rules_match_has_taxonomy( $match, $rule, $options )
    {
        if ($rule['operator'] == '==') {
            $match = is_object_in_taxonomy($options['post_type'], $rule['value']);
        } elseif ($rule['operator'] == '!=') {
            $match = !is_object_in_taxonomy($options['post_type'], $rule['value']);
        }
        return $match;
    }

    edit: add link to Gist

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Show field when taxonomy type is enabled’ is closed to new replies.