1

I'm querying a custom taxonomy for a post, hoping to get a list of all terms for that post and taxonomy. get_the_terms($post_id, $taxonomy) works, but gives this ungodly array of term objects that will require an extra layer of parsing before I can run the following code:

if (in_array($list_of_term_ids, $my_term_id)){ do good stuff... }

I'm looking to see whether anyone has come across a native WP function or method that extracts term ids into a flat list before rolling my own utility function, just so I can be using built-in best practices.

1
  • So I rolled my own map in the meantime; still would love to know if there's an "official" way of doing things (for forward-compatibility and all that). Here's the map, using a closure (PHP 5.3+) $term_ids = array_map(function($a){return $a->term_id;}, get_the_terms($post->ID, TAXONOMY_NAME)); You could of course do this with create_function instead: $term_ids = array_map(create_function('$a','return $a->term_id;'), get_the_terms($post->ID, TAXONOMY_NAME)); for php 4+. Still, if there's a better way, I'd love to hear it!
    – Tom Auger
    Commented Jun 9, 2011 at 18:09

2 Answers 2

6

I know you've long since solved this, but wanted to offer another solution. This question popped up as "related" when I was answering another one.

You can use the WordPress function wp_list_pluck to return an array with values as one of the fields of the array or objects sent to the function. In other words, send the function the objects and specify the field you want back and you'll get an array with only that field.

For instance, you can do something like:

$ids = wp_list_pluck(get_terms('category', 'hide_empty=0'), 'term_id'));

$ids will be an array of the terms ids that you wanted to capture.

3
  • Wow, very cool. More WP Goodness that's buried in there! Thanks for posting - I just learned something new! For the record, the solution I gave (and accepted) is the more idiomatic WP way of extracting a list of IDs from the taxonomy in question, however, if you need a more complex tax query, or sorting, then the method @tollmanz suggests is far more versatile. Nice one!
    – Tom Auger
    Commented Aug 23, 2011 at 14:56
  • I like to draw attention to wp_list_pluck as it's one of those hidden WordPress functions that does something so useful. Don't know how many times I wrote my own routine to do just that.
    – tollmanz
    Commented Aug 23, 2011 at 17:41
  • Awesome, you are getting upvote from me. I was searching for a method to get only array list of id's of categories so i can compare them to other array list, and wp_list_pluck perfectly does the trick.
    – lonerunner
    Commented Jul 21, 2016 at 23:12
0

Well, I had seen it and was thrown off by the first argument, but it does exactly what is needed. From wp-includes/taxonomy.php:

function wp_get_object_terms($object_ids, $taxonomies, $args = array())

And to use it as I wished, giving me a flat list of matching IDs, push 'fields'=>'ids' into $args, like so:

wp_get_object_terms($post_id, TAXONOMY_NAME, array('fields'=>'ids'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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