Getting two different answers when using get_the_terms
-
When setting a variable
$cats = get_the_terms( $current_page_id, 'category' );
Outputting
$cats
to the console gives me two slightly different objects depending on the post. (Usually after a while and a few refreshes they all look like Example 2, which is the expected output)One seems to be an array and the other an object with a key. Also, ex.1 also doesn’t include the “object_id” for some reason.
Example 1:
[{"term_id":8,"name":"Category Name","slug":"category-name","term_group":0,"term_taxonomy_id":8,"taxonomy":"category","description":"Category description.","parent":0,"count":26,"filter":"raw","cat_ID":8,"category_count":26,"category_description":"Category Description.","cat_name":"Category Name","category_nicename":"category-name","category_parent":0}]
Example 2:
{"8":{"term_id":8,"name":"Category Name","slug":"category-name","term_group":0,"term_taxonomy_id":8,"taxonomy":"category","description":"Category description.","parent":0,"count":26,"object_id":2403,"filter":"raw","cat_ID":8,"category_count":26,"category_description":"Category description.","cat_name":"Category Name","category_nicename":"gifts-that-give-back","category_parent":0}}
Has anyone ever encountered this? Any ideas why the difference?
NB: these have been converted to JSON for outputting into the console. Can’t simply use var_dump or print_r in this case.
- The topic ‘Getting two different answers when using get_the_terms’ is closed to new replies.