WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I get only the first term of a custom post type.
I can get all - no problem. This what I am using to grab all of them

<?php foreach ($terms as $term) {echo '<a href="'.get_term_link($term->slug, 'sitecat').'">'.$term->name.'</a>,';} ?> >> <a href="<?php the_permalink(); ?>"><?php the_title('', ''); ?></a></h2></span>

Would appreciate an answer using my code but any help is most welcomed

share|improve this question
up vote 12 down vote accepted

I'm not sure what you mean by 'first' taxonomy... but,

$terms = get_the_terms( $post->ID, 'mytaxonomy' );

returns an array of taxonomy term objects, so

$term = array_pop($terms);

Would give you the first term in the array. And then:

echo '<a href="'.get_term_link($term->slug, 'mytaxonomy').'">'.$term->name.'</a>,'

(You may want to include some if statements, in case an empty array or error is returned (see is_wp_error)

share|improve this answer
just tried this.. getting an error: Catchable fatal error: Object of class WP_Error could not be converted to string in /home/content/14/6469114/html/wp-content/themes/sagive/single-website.php on line 40 – Sagive SEO Mar 10 '12 at 3:10
on line 40: <?php $terms = get_the_terms( $post->ID, 'sitecat' ); array_pop($terms); echo '<a href="'.get_term_link($term->slug, 'sitecat').'">'.$term->name.'</a>'; ?> – Sagive SEO Mar 10 '12 at 3:11
ok.. the error was on my side - not your answer ;) thanks a lot mate – Sagive SEO Mar 16 '12 at 20:34

As of PHP 5.4, you can directly dereference an array, so to get the first term, you can simply do.

$first_term = get_the_terms( $post->ID, 'TAXONOMY_NAME' )[0];
var_dump( $first_term );

If you need a specific property (say the term name) of the first term, you can do the following

$first_term_name = get_the_terms( $post->ID, 'TAXONOMY_NAME' )[0]->name;
var_dump( $first_term_name );


Just a note, this does have its draw backs because you will get a WP_Error object if the taxonomy is invalid. Also, if the returned array is empty, you will also get an undefined array key warning, so use this with with care.

share|improve this answer
Good edit, thanks for taking the time ;) – Sagive SEO Jul 28 '15 at 23:16

It worked for me. It brings only the first category as text, no anchor.

$terms = get_the_terms( $post->ID , 'your_custom_taxonomy' ); 
foreach( $terms as $term ) { 
    print $term->name;
share|improve this answer
What "worked for you"? Sidenote: The break; will really stop your statement, the unset will never be triggered and only the 1st item will be processed. – kaiser Mar 4 '14 at 12:57
unset( $term ); should be after your foreach loop, and not inside it :-) – Pieter Goosen Jul 28 '15 at 8:18

how about directly access the key of object?

$terms = get_the_terms( $post->ID, 'sitecat' );
$first_term = $terms[0];

so you can access the object.

echo $first_term->name;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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