Support

Account

Home Forums Front-end Issues Field group for Taxonomy Term MIA

Solved

Field group for Taxonomy Term MIA

  • I’m trying to follow the docs with adding custom fields to a specific category but I’m just not getting what the same results as the screenshots.

    Location by Taxonomy Term

    I’m instructed to select:
    Location -> Show this field group if -> Taxonomy Term and yet it’s nowhere to be found.

    Selecting:
    Location -> Show this field group if -> Post -> Post Category or Location -> Show this field group if -> Post -> Post Taxonomy give me no results, and Forms -> Taxonomy adds it to everything! (Category page AND custom Category)
    Is there something that I’m overlooking?

  • What version of ACF are you using?

    The latest version of ACF does not have a “Taxonomy Term” location rule, only a “Taxonomy”. The doc is old. Choose “Taxonomy is equal to {the taxonomy you want the fields on}”

  • I’m running the newest version (5.12.3)

    I had a suspicion that it might have been old documentation, but with only the Taxonomy location specified, it adds it too many places.

    Application of Taxonomy location
    Category Page
    Custom Category

  • Serving as an update to my reply from yesterday, I thought that since adding the fields to only the Taxonomy -> Category was too general, increasing the specificity with an addition Location option would be the best next move. But everything gets completely removed. @hube2 would you have any other recommendations? Or is this an actual bug?

    Increasing Location specificity
    I’ve also tried with Post Taxonomy instead of Post Category:
    Increasing Location specificity with Post Taxonomy
    No fields added to default category page
    Also no fields added to custom category page

  • I don’t see anything wrong with the original location rules

    Taxonomy is equal to category should put the fields on the term editor page when ediging any category.

    What other plugins are you using. The only thing I can think of if it is adding fields to other categories is some type of conflict.

  • Maybe I’m misunderstanding the abilities of targeting by taxonomy. Is it only for adding fields to ALL categories? No way of having fields available only on SPECIFIC categories?

    For instance in my example above, I have two categories: Campuses and Cities, but I only want custom fields on my Campuses category. Can I target that granular?

  • Yes, you can only add fields to all terms in a taxonomy, not specific terms. Do do this you’d need to create a custom location rule. Sorry, I don’t have any code for this and I can’t find any. I’m sure that there is an example of this on this forum, or a link to it, but I can’t seem to locate it.

  • This is it. Thank you so much @hube2. I didn’t even know this was possible! 🤘🏻

  • Here’s my code for anyone that this might help.

    This is my solution to specifically targeting children of parent categories:
    i.e.:

    • Locations

      • Los Angeles, CA
      • Denver, CO
      • New York, NY

    (Credit to Kellen Mace for a snippet to identify the current taxonomy admin page. Link in the code.)

    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    
    class ACF_Category_Name_Location extends ACF_Location {
        public function initialize() {
            $this->name         = 'category_name';
            $this->category     = __( 'Category', 'acf');
            $this->label        = __( 'Category Name', 'acf' );
            $this->object_type  = 'post';
        }
    
        public function get_values( $rule ) {
            $choices = array();
    
            // Load all category terms, loop over them and append to choices.
            $terms = get_terms( array(
                'taxonomy' => 'category',
                'orderby' => 'name',
                'hide_empty' => false,
                'parent' => 0,
            ) );
    
            if( $terms ) {
                foreach( $terms as $term ) {
                    $choices[ $term->term_id ] = $term->name;
                }
            }
    
            return $choices;
        }
    
        public function match( $rule, $screen, $field_group ) {
            /**
             * https://kellenmace.com/get-term-edited-term-php-wordpress-admin-page/
             */
            // Get the term currently being edited on the edit.php screen in the admin.
            global $taxnow;
    
            if ( ! $taxnow || empty( $_GET['tag_ID'] ) ) {
                return false;
            }
    
            $term_id = absint( $_GET['tag_ID'] );
    	    $term    = get_term( $term_id, $taxnow );
    
            // Return false if not WP_Term
            if ( ! $term instanceof WP_Term ) {
                return false;
            }
    
            // Compare the current term parent to rule value.
            $result = ( $term->parent == $rule['value'] );
    
            // Return result taking into account the operator type.
            if( $rule['operator'] == '!=' ) {
                return !$result;
            }
    
            return $result;
        }
    }
  • Here’s my code if it’s of any help to others.

    This is my solution to adding custom fields targeting specifically the children of parent categories.
    i.e.:

    • Locations

      • Los Angeles, CA
      • Denver, CO
      • New York, NY

    (Credit to Kellen Mace for the snippet on finding the current category edit page. Link in the code.)

    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly
    }
    
    class ACF_Category_Name_Location extends ACF_Location {
        public function initialize() {
            $this->name         = 'category_name';
            $this->category     = __( 'Category', 'acf');
            $this->label        = __( 'Category Name', 'acf' );
            $this->object_type  = 'post';
        }
    
        public function get_values( $rule ) {
            $choices = array();
    
            // Load all category terms, loop over them and append to choices.
            $terms = get_terms( array(
                'taxonomy' => 'category',
                'orderby' => 'name',
                'hide_empty' => false,
                'parent' => 0,
            ) );
    
            if( $terms ) {
                foreach( $terms as $term ) {
                    $choices[ $term->term_id ] = $term->name;
                }
            }
    
            return $choices;
        }
    
        public function match( $rule, $screen, $field_group ) {
            /**
             * https://kellenmace.com/get-term-edited-term-php-wordpress-admin-page/
             */
            // Get the term currently being edited on the edit.php screen in the WP admin.
            global $taxnow;
    
            if ( ! $taxnow || empty( $_GET['tag_ID'] ) ) {
                return false;
            }
    
            $term_id = absint( $_GET['tag_ID'] );
    	    $term    = get_term( $term_id, $taxnow );
    
            // return false if not and instance of WP_Term
            if ( ! $term instanceof WP_Term ) {
                return false;
            }
    
            // Compare the selected terms parent to rule value.
            $result = ( $term->parent == $rule['value'] );
    
            // Return result taking into account the operator type.
            if( $rule['operator'] == '!=' ) {
                return !$result;
            }
    
            return $result;
        }
    }
    
  • Your code was very helpful but I couldn’t get it to work until I amended it –>

    from:

    // Compare the selected terms parent to rule value.
     $result = ( $term->parent == $rule['value'] );

    to:

    // Compare the selected terms term_id to rule value.
     $result = ( $term->term_id == $rule['value'] );
Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.