I'm currently storing a string of comma separated taxonomy IDs in a text field for each user. I have managed to retrieve the string from a user which I've saved in a variable called '$user_terms'. I can split this string into an array of IDs called '$user_terms_array. I'm stuck now though as I need to get the name of the IDs. I found this:


but I'm not particularly sure how to use this/ if I can use this in my situation. I also found this:


But again I can't quite figure out how to use this. I tried the following:

$context['single_term'] = get_term( $user_terms_array[1], $taxonomy = 'terms' );

Then in my twig template if I try to output the value of single_term I get the following output in the HTML:


Object Caching 4429/6070 objects using disk Page Caching using disk: enhanced (User is logged in) Database Caching using disk (Request-wide User is logged in)

Served from: dev.url @ 2017-08-22 10:28:00 by W3 Total Cache -->'

Not quite sure where I'm going wrong.


I tried this: $context['single_term'] = get_term( $user_terms_array[1], $taxonomy = 'terms' )->name; and it get no output, it's just blank.


First, you can't enter such expression as second argument in get_term(). Enter directly the name of your taxonomy...
Are you dealing with simple post categories? If that's the case, your taxonomy name is 'category'.

$context['single_term'] = get_term( $user_terms_array[1], 'category' );

Second, ensure that $user_terms_array contains term IDs as integer.

$user_terms_array = array_map( 'intval', explode( '-', $user_terms ) );

Then, you're on the good way. Using WP_Term_Query instead of get_term() wouldn't give you any pros if I refer to the supplied info. In this context, I'd stick with get_term().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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