I hope this is alright to ask about on here, and I'm going to phrase it from a pure WordPress perspective, my dear forum moderators. I've previously asked the author. Plus I think it's a good, unique question for posterity.

I'm using a Select2 plugin with CMB2 and there is a function that allows you to query taxonomies (normally it just allows manually entered arrays).

OK, that's great, right? Yes, except the output if you wanted to place the selected data on a post, for example, is only the term ids. And here is where it gets a little trickier and why I am soliciting help...

...it doesn't actually connect back to the taxonomy when data is selected.

So to summarize the process:

  1. Terms are queried into the select2 field via a function
  2. The output after a selection is made are term ids, but they aren't looped back to the taxonomy. Let's call these objects.

What I'm trying to figure out: A function that says "if an object = term id, then return term name".

Also- the returned object is a numeric array, which I can easily implode. Plus I know WordPress automatically identifies individual metadata in an array ([0],[1], etc)


Edit: Figured it out like a boss. Answer below.


Great googly moogly, I figured this one out.

 $values = get_post_meta( get_the_ID(), 'your_meta_whatever', true ); 
 foreach ($values as $value) 
 {$term = get_term_by('term_id', $value, 'your taxonomy'); 
 $name = $term->name echo $name;}

If you want to keep 'em separated by commas or the characters your choice

 $values = get_post_meta( get_the_ID(), 'your_meta_whatever', true );
 $total = count($values);$i=0; foreach ($values as $value) {$i++; 
 $term = get_term_by('term_id', $value, 'your taxonomy'); 
 $name = $term->name; 
 echo $name; 
if($i !=$total) echo', ';}

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.