Title: Problem using get_the_terms
Last modified: February 27, 2017

---

# Problem using get_the_terms

 *  Resolved [Zoinks! Graphics](https://wordpress.org/support/users/zoinks/)
 * (@zoinks)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/)
 * I’ve been trying to sort image displays on the front end using the ‘media_category’(
   default) image category custom taxonomy. However, any time I use the code below
   it either returns empty values (best case) or throws a major WP error and breaks
   my page. Any ideas what I might be doing wrong?
 *     ```
       $imageCategories = get_the_terms($imageID,'media_category');
   
       foreach($imageCategories as $category) { $catArray[] = $category->name; }
       $labelstring = join(", ", $catArray);
       ```
   

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Plugin Author [webbistro](https://wordpress.org/support/users/webbistro/)
 * (@webbistro)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8851752)
 * Hello [@zoinks](https://wordpress.org/support/users/zoinks/),
 * First, `get_the_terms` returns `(array|false|WP_Error)`, see [https://developer.wordpress.org/reference/functions/get_the_terms/](https://developer.wordpress.org/reference/functions/get_the_terms/),
   you are trying to address it as an object.
 * Second, before `foreach($imageCategories as $category)` you have to check if `
   $imageCategories` is not `false` or `WP_Error`.
 * Third, it may be empty because `$imageID` is not a correct ID for your image,
   for example.
 * Best,
    -Nadia
 *  Thread Starter [Zoinks! Graphics](https://wordpress.org/support/users/zoinks/)
 * (@zoinks)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8851794)
 * Thanks for the super-speedy response, Nadia!
 * First, I do run the result of get_the_terms() through foreach($imageCategories
   as $category) so that should “de-array” it, no?
 * Second, yes, I’ve been putting that “if not empty and no error” variable, which
   many times causes _nothing_ to be output. That’s the heart of the problem, get_the_terms()
   using ‘media_category’ is either generating nothing or errors.
 * Third, sorry, I left out that yes, I am assigning $imageID the actual image ID
   in the loop via $picID=get_the_ID(); And yes, it does generate valid image IDs
   that I’ve confirmed in the Media section.
    -  This reply was modified 9 years, 2 months ago by [Zoinks! Graphics](https://wordpress.org/support/users/zoinks/).
 *  Plugin Author [webbistro](https://wordpress.org/support/users/webbistro/)
 * (@webbistro)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8851836)
 * Please post the result of `var_dump( $imageCategories )`.
 *  Thread Starter [Zoinks! Graphics](https://wordpress.org/support/users/zoinks/)
 * (@zoinks)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8855148)
 * Thanks again! Full code is…
 *     ```
       $images = array(271, 279, 291, 326, 331, 494, 496, 498, 3041, 3043, 3044, 3045);
   
       foreach($images as $imageID)
       	{
       	unset($labelarray);
   
       	$imageCats = get_the_terms($imageID,'media_category');
       	var_dump($imageCats); // ADDED YOUR var_dump()
   
       	if($imageCats && !is_wp_error($imageCats))
       		{
       		foreach($imageCats as $cat) { $catArray[] = $cat->name; }
       		$string = join( ", ", $catArray );
       		}
       	else { $string=$imageID; }
   
       	echo '<div style="text-align:center;">' . $string . '</div>';
       	}
       ```
   
 * With your var_dump($imageCats) added resulted in…
 * > object(WP_Error)#1739 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]=> 
   > array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } }
   >  271 object(WP_Error)#1097 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 279 object(WP_Error)#1739 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 291 object(WP_Error)#1097 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 326 object(WP_Error)#1739 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 331 object(WP_Error)#1097 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 494 object(WP_Error)#1739 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 496 object(WP_Error)#1097 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 498 object(WP_Error)#1739 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 3041 object(WP_Error)#1097 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 3043 object(WP_Error)#1739 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 3044 object(WP_Error)#1097 (2) { [“errors”]=> array(1) { [“invalid_taxonomy”]
   > => array(1) { [0]=> string(17) “Invalid taxonomy.” } } [“error_data”]=> array(
   > 0) { } } 3045
 *  Thread Starter [Zoinks! Graphics](https://wordpress.org/support/users/zoinks/)
 * (@zoinks)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8855165)
 * And yes, the $images to start is an array of valid image IDs under Media.
 * And here are snapshots of my “Media Library” and “Media Taxonomies” settings…
 * ![Media Library settings](https://i0.wp.com/zoinks.graphics/images/Screen%20Shot%
   202017-02-27%20at%208.03.10%20PM.png)
 * ![Media Taxonomies settings](https://i0.wp.com/zoinks.graphics/images/Screen%
   20Shot%202017-02-27%20at%208.03.34%20PM.png)
 *  Plugin Author [webbistro](https://wordpress.org/support/users/webbistro/)
 * (@webbistro)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8855633)
 * Hello [@zoinks](https://wordpress.org/support/users/zoinks/),
 * Well, it says “Invalid taxonomy.” You are trying to get the terms for taxonomy
   before it is being registered. What hook (or template) do you use to insert the
   code you provided?
 * Best,
    -Nadia
    -  This reply was modified 9 years, 2 months ago by [webbistro](https://wordpress.org/support/users/webbistro/).
 *  Thread Starter [Zoinks! Graphics](https://wordpress.org/support/users/zoinks/)
 * (@zoinks)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8858521)
 * Ahhh, now you’ve pointed me in the right direction… It hadn’t occurred to me 
   that I’m firing this before your plugin registers the taxonomy, more fool me!
   Let me do some digging into how to get this to register AFTER that. Thank you!!
    -  This reply was modified 9 years, 2 months ago by [Zoinks! Graphics](https://wordpress.org/support/users/zoinks/).
 *  Plugin Author [webbistro](https://wordpress.org/support/users/webbistro/)
 * (@webbistro)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8858552)
 * Hello [@zoinks](https://wordpress.org/support/users/zoinks/),
 * This may be helpful: Enhanced Media Library registers media taxonomies on `init`
   action, and assigns taxonomies created by third parties on `wp_loaded` action.
 * Best,
    -Nadia
    -  This reply was modified 9 years, 2 months ago by [webbistro](https://wordpress.org/support/users/webbistro/).
    -  This reply was modified 9 years, 2 months ago by [webbistro](https://wordpress.org/support/users/webbistro/).
    -  This reply was modified 9 years, 2 months ago by [webbistro](https://wordpress.org/support/users/webbistro/).
 *  Thread Starter [Zoinks! Graphics](https://wordpress.org/support/users/zoinks/)
 * (@zoinks)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8858649)
 * Thank you SO much, Nadia!! Here’s the completed test code, which will allow me
   to create my ACTUAL code…
 *     ```
       if(is_zg() && !is_admin())
       	{ add_action('wp_loaded','zg_after_plugins_check'); }
   
       function zg_after_plugins_check()
       	{
       	// echo zg_random_default_image(1);
   
       	$images = array(271, 279, 291, 326, 331, 494, 496, 498, 3041, 3043, 3044, 3045);
   
       	foreach($images as $imageID)
       		{
       		unset($labelarray);
   
       		$imageCats = get_the_terms($imageID,'media_category');
       		// var_dump($imageCats);
   
       		if($imageCats && !is_wp_error($imageCats))
       			{ foreach($imageCats as $cat) { $catArray[]=$cat->name; } }
       		else { $catArray[]=$imageID; }
       		}
   
       	$string = join( ", ", $catArray );
       	echo '<p>' . $string . '</p>';
       	}
       ```
   
 * “is_zg()” is my custom code to check for logged in as “super-admin” (me)
 * Thanks again!!
 *  Plugin Author [webbistro](https://wordpress.org/support/users/webbistro/)
 * (@webbistro)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8858664)
 * You are welcome! Glad to know your issue is resolved.
 * Best,
    -Nadia

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Problem using get_the_terms’ is closed to new replies.

 * ![](https://ps.w.org/enhanced-media-library/assets/icon-256x256.png?rev=980107)
 * [Enhanced Media Library](https://wordpress.org/plugins/enhanced-media-library/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/enhanced-media-library/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/enhanced-media-library/)
 * [Active Topics](https://wordpress.org/support/plugin/enhanced-media-library/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/enhanced-media-library/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/enhanced-media-library/reviews/)

 * 10 replies
 * 2 participants
 * Last reply from: [webbistro](https://wordpress.org/support/users/webbistro/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/problem-using-get_the_terms/#post-8858664)
 * Status: resolved