Get Advance Custom Fields Value from Taxonomy Term

Recently when I was working on some project that have includes some custom post-types, custom taxonomy and terms. While developing a specific template that retrieve  some advance custom fields value from specific terms under custom taxonomy and I was trying to get the solution. Then I found that advance custom fields can get fields value from any custom taxonomy term same as from some post ID. The difference is only that instead of   using post ID you can use taxonomy name followed by an underscore, followed by the term ID instead of the post ID when calling the_field or get_field.

Term Object

The term object can be passed as the $post_id parameter as of ACF v4.3.3. This is the most simple and efficient way to load a value from a term object. You can get a term object via many of WP’s functions such as get_term.

Term String

The term string can be passed as the $post_id parameter and is constructed from the term’s taxonomy and the term’s ID. In code, this looks like ”{$term->taxonomy}_{$term->term_id}”


Example

Let assume that you have Custom post type named “Cars” and its have taxonomy  named “models” and  some terms under above taxonomy e.g BMW and Audi. Now What we need first is to create a custom post type. See the code below for creating custom post type named “cars”

You can add this code to your functions.php file in child-theme and customize it according to your needs. e.g you can change menu location, labels and slug as well. Feel free to ask anything about code and I will explain more.  Next task is to create a taxonomy under this custom post type “cars”. Don’t  worry I have code snippet ready for you to just copy and paste. See below code for creating custom taxonomy linked with custom post type “cars”

Just add above code in functions.php in child-theme and it will display a menu inside WordPress admin panel under Cars. As you can see i have selected false in front of “hierarchical” so its will display terms same as tags, but if you want it hierarchical like categories than you can change it to true.

Again here you can customize whatever you need either labels, slug or taxonomy name according to your need. Next step is creating a custom field in Advance Custom Fields and you already know we have a super plugin for that purpose named Advanced custom fields plugin, thanks to Elliot Condon who created and maintain this awesome plugin.

I have create a custom field named “model_year” inside a group and then assign a location of that field to only appear on selected taxonomy terms. In this case I have selected models and this field only display all terms under this taxonomy model.

create advance custom field
  • Facebook
  • Twitter
  • Google+
  • LinkedIn
  • Pinterest

Create Advance Custom Field

After this just assign the location of custom field to desired taxonomy like below

Assign advance custom field location
  • Facebook
  • Twitter
  • Google+
  • LinkedIn
  • Pinterest

Assign advance custom field location

We have successfully created the custom post type, custom taxonomy and advance custom field to display on taxonomy terms. Next question is how we can get the value of advance custom field from specific term. As we have assumed we have two terms under taxonomy models. BMW and Audi. Now for example we need to get custom field value only from Audi. Here is the simple code for you

In above code we have added field name model_year, taxonomy name models and term ID 4. Just use this in any custom template you need and it will display the value only from specific term ID. You can also do it with variables

Retrieving a field as a variable

and it’s not limited because sky is a limit.

Share your valuable feedback and ideas how you have done this and suggestion for improvements are most welcome. There are many more ways to do this so feel free to share if you know how to do that.

Web consultant and Web Designer with over 7 years of professional and creative experience. Jamil is passionate about creating modern and user friendly websites that help businesses attract more customers and increase sales. He received bachelor and master in Business Administration and also co-founder of www.prepareddevelopment.com

Pin It on Pinterest

Share This