[Solved] Get Array of Terms for Custom Post Type Outside of Loop with Post ID

March 7, 2017

Every stone I overturned looking for the answer to this issue I was met with “use wp_get_post_terms()” but unfortunately it does not work on custom post types.

The solution, after much digging and experimenting is to use get_the_term_list() but with a couple of twists.

Now, I have a custom post type called “faq”.

To get the “terms” or “tags” for the “faq” post type you need to know what the “taxonomy name” is. In this case, the taxonomy name is “faqs-tags”.

//this will get a list of the tags in csv format (notice use of strip_tags)
$csv_of_tags = strip_tags(get_the_term_list( $post_id, 'faqs-tags', '', ', ' ));

//if some tags exist convert the csv list into an array of tags using explode()
if(strlen($csv_of_tags) > 0) {
  $array_of_tags = explode(',', $csv_of_tags);

//Output of $array_of_tags will be in the form of a php array. Sweet! 
    [0] => car hire
    [1] => ECC
    [2] => European Consumer Centre
    [3] => holidays
    [4] => purchases
    [5] => travel

To find out the taxonomy name for the custom post type, check the screenshot below. Easy!