I'm working on an extra function to list different stuff,

My function looks like: "display all hotels for a specific city" -> hotels is as CPT and CITY as custom Tax. So for exemple, lets display all hotels in Paris.

function test($cpt,$taxonomy = 0) {
   $args (...)
   $query (...)
   while {...}

So I go to query different custom posts types, with or without a taxonomy. Until here, everything is OK.

But is it possible to get some data from this $taxonomy (city/paris) without creating a new query? Like I want to get custom field called "area_singular" linked to the $taxonomy passed in the function? The title of it, etc.?

P.S. The value of $taxonomy is passed by

$term = get_queried_object();

P.P.S. Actually i have an other function, where i pass this custom field, & title in the function like test('cpt',$term,$custom_field,$title); but I'm sure there is a way to get the data without passing them by the function.

EDIT: With $taxonomy->post_title I can get the title, so how can I get the custom field the same way?

  • Your question is a bit unclear. Are you trying to get the value of a field attached to a custom taxonomy term? Example: advancedcustomfields.com/resources/adding-fields-taxonomy-term/… – MikeNGarrett Mar 9 '19 at 19:31
  • Hello, yes, it is a custom taxonomy term. Its like, "i want to display all hotels from paris" ( hotels = cpt, paris = custom tax). But before i make my query + while, i want to display a custom field, linked to paris. Is this more clear ? – Gregory Mar 9 '19 at 19:35
  • ok, i arrive to get title with $taxonomy->post_title. Now i need to get my custom field and it will be perfect ^^ – Gregory Mar 9 '19 at 21:17

According to the ACF documentation on getting field data from fields attached to terms, you want to do something like this:

$term = get_queried_object();    
$area_singular = get_field( 'area_singular', $term );
  • yes, i know this method, but it returns nothing :/ – Gregory Mar 9 '19 at 21:09
  • it returns "null" to be exact (with var_dump()) – Gregory Mar 9 '19 at 21:12
  • That either means the $term is not a term object or the area_singular field isn't associated with the term you're requesting. I recommend taking a look at the database to see if this is the case. – MikeNGarrett Mar 10 '19 at 16:44
  • Some of your comments have been a bit confusing, so I want to take a step back and make sure I understanding how you have this set up. In the admin, is the area_singular field able to be edited from the edit term page or from the edit post page? – MikeNGarrett Mar 10 '19 at 16:47
  • From the taxonomy. – Gregory Mar 10 '19 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.