Support » Plugin: Enhanced Media Library » 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

    (@webbistro)

    Hello @zoinks,

    First, get_the_terms returns (array|false|WP_Error), see 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

    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.

    Plugin Author webbistro

    (@webbistro)

    Please post the result of var_dump( $imageCategories ).

    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

    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

    Media Taxonomies settings

    Plugin Author webbistro

    (@webbistro)

    Hello @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 2 years, 6 months ago by  webbistro.

    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!!

    Plugin Author webbistro

    (@webbistro)

    Hello @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 2 years, 6 months ago by  webbistro.
    • This reply was modified 2 years, 6 months ago by  webbistro.
    • This reply was modified 2 years, 6 months ago by  webbistro.

    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

    (@webbistro)

    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.