0

I have some custom taxonomies that I would like to use as an object in a foreach loop.

I know these functions which returns the object for either all taxonomies:

global $wp_taxonomies;

or the post taxonomies object:

$taxonomy_objects = get_object_taxonomies( 'post', 'objects' );

but if I would like to only include a few taxonomies in the object like video, audio, tv and radio (all taxonomy names), how would I go about it?

1 Answer 1

2

I am not aware of a function to get only specific term objects for a post type but it is not hard to filter out the ones you want. For example:

$taxonomies = get_object_taxonomies( 'post', 'objects' );
$taxonomies = array_intersect_key($taxonomies,array_flip(array('category','post_tag')));
var_dump($taxonomies);

But since you know the taxonomies you want, you could also just use get_taxonomy( $taxonomy ) multiple times.

$tax_objects = array();
$taxlist = array(
  'category',
  'post_tag'
);
foreach ($taxlist as $tax) {
  $tax_objects[$tax] = get_taxonomy($tax);
}
var_dump($tax_objects);

Or you may be making this more complicated than it needs to be. You can just simply use the taxonomies you need:

$taxonomies = get_object_taxonomies( 'post', 'objects' );
if (!empty($taxonomies['category'])) {
  var_dump($taxonomies['category']);
}

Obviously, all code above uses Core taxonomies (so I can test things) but you can easily swap those out for your own specialized taxonomies.

1
  • That's a great answer for the "average Joe". Simple but a very helpful tutorial. I have lost count of the number of times you helped me out here. Thanks a lot. I use this to filter out some metaboxes in the edit post admin page by means of the "Order bender" plugin and with get_taxonomy() , as you suggested, the plugin will no longer display all the metaboxes, only those I need.
    – psot
    Apr 25, 2014 at 14:11

Your Answer

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

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