Support

Account

Forum Replies Created

  • 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;
        }
    }
    
  • 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;
        }
    }
  • This is it. Thank you so much @hube2. I didn’t even know this was possible! 🤘🏻

  • 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?

  • 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’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

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