Support

Account

Home Forums General Issues How to add custom field to a category?

Solved

How to add custom field to a category?

  • I would have thought this was simple given the options available when creating a new field group.

    I am using WooCommerce and create a couple of categories called “Grade 11” and “Grade 12”.

    In ACF, the group I’m in has “show this field group if” Post Taxonomy is equal to Grade 11 and Grade 12. Post Category didn’t return these categories, which is why I used Post Taxonomy.

    I created a field called “Grade” that uses the input type “Number”.

    I clicked on the WooCommerce categories and I didn’t notice the field in the creation process. I also clicked on an already made category and didn’t see it there neither.

    What am I doing wrong?

  • Can you give me a screenshot of your location rules?

  • http://imgur.com/a/xZd70

  • You have it set to AND.

    Delete the second rule and then click the “Add Rule Group” button and add it there.

  • Still doesn’t appear, that’s what I had originally and then tried the “and”.

  • Duh, my fault. That will only let the field group appear on posts in that category. There isn’t a location rule that exists for adding field groups to specific terms (term editor page), only for all of the terms in a taxonomy.

    You need a custom location rule for this https://www.advancedcustomfields.com/resources/custom-location-rules/. This is a fairly common one, but I don’t have an example. I’m sure that somewhere on this forum there is an example for this location rule but I can’t seem to find it right now.

  • Hmm, this seems pretty confusing to me. Out of my depth for sure.

  • Anyone mind helping me out, I read the link above but I’m still unsure about how to create this rule.

  • So, I’m in the middle of seeing if I can help you with this when I realize, I’m not really sure exactly what page you want this field group to appear on. Can you post the URL of an example page in the admin where you want it to appear? You can strip of the domain, I just need to see the part that starts /wp-admin/…

  • /wp-admin/term.php?taxonomy=product_cat&tag_ID=15&post_type=product&wp_http_referer=%2Fdemo%2Fchla%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dproduct_cat%26post_type%3Dproduct

    /wp-admin/term.php?taxonomy=product_cat&tag_ID=16&post_type=product&wp_http_referer=%2Fdemo%2Fchla%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dproduct_cat%26post_type%3Dproduct

  • That’s what I thought, I’ll see what I can do. steps 1 to 3 are easy, it’s step 4 that’s the complicated part.

  • Yeah, I got the first dropdown to appear, but the product categories got tricky.

  • 
    <?php 
      
      
      // step 1 add a location rule type
      add_filter('acf/location/rule_types', 'acf_wc_product_type_rule_type');
      function acf_wc_product_type_rule_type($choices) {
        // first add the "Product" Category if it does not exist
        // this will be a place to put all custom rules assocaited with woocommerce
        // the reason for checking to see if it exists or not first
        // is just in case another custom rule is added
        if (!isset($choices['Product'])) {
          $choices['Product'] = array();
        }
        // now add the 'Category' rule to it
        if (!isset($choices['Product']['product_cat'])) {
          // product_cat is the taxonomy name for woocommerce products
          $choices['Product']['product_cat_term'] = 'Product Category Term';
        }
        return $choices;
      }
      
      // step 2 skip custom rule operators, not needed
      
      
      // step 3 add custom rule values
      add_filter('acf/location/rule_values/product_cat_term', 'acf_wc_product_type_rule_values');
      function acf_wc_product_type_rule_values($choices) {
        // basically we need to get an list of all product categories
        // and put the into an array for choices
        $args = array(
          'taxonomy' => 'product_cat',
          'hide_empty' => false
        );
        $terms = get_terms($args);
        foreach ($terms as $term) {
          $choices[$term->term_id] = $term->name;
        }
        return $choices;
      }
      
      // step 4, rule match
      add_filter('acf/location/rule_match/product_cat_term', 'acf_wc_product_type_rule_match', 10, 3);
      function acf_wc_product_type_rule_match($match, $rule, $options) {
        if (!isset($_GET['tag_ID'])) {
          // tag id is not set
          return $match;
        }
        if ($rule['operator'] == '==') {
          $match = ($rule['value'] == $_GET['tag_ID']);
        } else {
          $match = !($rule['value'] == $_GET['tag_ID']);
        }
        return $match;
      }
      
      
    ?>
    
  • YOU ARE A WIZARD.

    I spent hours looking at other examples and trying on my own. Thank you so much!!

  • $grade = get_field(“grade”);

    For some reason I can’t get this to return anything. Do I need something special to call this given that it’s a custom location rule?

  • I also tried:

    $grade = get_field(“grade”, “product_cat_term”);
    $grade = get_field(“grade”, “product_cat”);

    Not sure what I need to make this output correctly.

  • you need the term ID

    
    $queried_object = get_queried_object();
    
    // ACF5 >= 5.5
    get_field('field_name', 'term_'.$queried_object->term_id');
    
    // ACF < 5.5
    // get_field('field_name', $queried_object->taxonomy.'_'.$queried_object->term_id');
    
  • $queried_object = get_queried_object();
    $grade = get_field("grade", "term_" . $queried_object->term_id);

    I had to remove the last “‘” you had in order for my editor to show the concatenated variable. This doesn’t work for me though.

  • $queried_object = get_queried_object();
    	        $taxonomy = $queried_object->taxonomy;
    			$term_id = $queried_object->term_id;  
    
    			$grade = get_field('grade', $queried_object);
    			$grade = get_field('grade', $taxonomy . '_' . $term_id);
    
    			var_dump($grade);

    The var_dump shows “NULL NULL NULL”

  • You’ll need to look at how to get the term for the template of in the code you’re modifying then. But this is the right syntax to use

    
    $grade = get_field('grade', $taxonomy . '_' . $term_id);
    
    • hilt

    • March 28, 2017 at 5:29 pm

    Hi

    i have tried to show an image and text block on woocommerce category page . I have tried almost every example .

    $queried_object = get_queried_object();
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;

    $grade = get_field(‘grade’, $queried_object);
    $grade = get_field(‘grade’, $taxonomy . ‘_’ . $term_id);

    this code will showing me only the following output

    6395, , category-ballons, , , image/png, https://i2.wp.com/kxxxxxx.com/demo/wp-content/uploads/2016/09/category-ballons.png?fit=1920%2C400, 1920, 400, Array

    any of the solution didn’t works for me .

    • a a

    • March 30, 2017 at 9:06 am

    Hi, I spent many hours whit this issue and I can’t find a solution.
    As I see there are more people trying to deal with that.

    The problem is that the proposed solution of using:

    $grade = get_field('grade', $taxonomy . '_' . $term_id);

    is not working in our case.

    I’m also trying to use the code above passing the data as string but the retrieved value is ever NULL.

    $title = get_field('my_title', 'dc_vendor_shop_5');

    I’m using ACF with Woocommerce and wc-marketplace.com Multivendor plugin

    Could someone help with this issue?

    Thanks.

  • The problem here is that there isn’t anyone that is familiar enough with WC or WC templates. What you need to do is find out from WC or from someone that knows enough about WC to get the taxonomy and term. I personally have not worked with WC so can not really help you much with this.

    You’re using 2 different plugins here and for the most part you’re only going to find help with the ACF part of it here. I’m willing to help you figure out how to show the values of your fields if you do the legwork to figure out how to get the term in the template where you are modifying code, or how to get the term inside of the WC filter that you need to use to add your custom code.

    Please also note that this is only for ACF4 now. ACF5 Pro (5.5.0) has implemented term meta which means that you and use either the "{$taxonomy}_{%term_id}" method or your can now use "term_{$term_id}"

    As far as the odd output of the contents of a field, please see the example code provide in the documentation section of this site. Most field types except the basic field types, need you do to more than echo their values depending on what you have them set to return.

    • a a

    • March 31, 2017 at 1:29 am

    Hi John, and thank you for your answer.

    I’ve just posted a question into the support forum of the plugin’s developers, so maybe we’ll reach some info from there.

    On the other hand I think I don’t fully understand what you are saying about term metas. I’m using the last version of ACF PRO.

    Unfortunately I’m just starting with WP development so there are some concepts I don’t understand at 100%.

    Thank you,

    Antonio

  • If you’re using the latest version of ACF Pro then you don’t need to worry about my comment about term meta vs options.

    It is possible after ACF 5.5.0 to get custom fields on terms differently. You can use the $post_id listed on this page https://www.advancedcustomfields.com/resources/get_field/ under the Get a value from a different object section. The new value for $post_id is detailed in this blog update from the developer https://www.advancedcustomfields.com/blog/acf-pro-5-5-0-update/. You can currently use either method for determining the $post_id method to use when getting values.

Viewing 25 posts - 1 through 25 (of 33 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.