11

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

5 Answers 5

30

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)

1
  • ok.. the error was on my side - not your answer ;) thanks a lot mate
    – Sagive
    Mar 16, 2012 at 20:34
7

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 );

EDIT

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.

1
  • How would i get all term id's foreach post for use on the home age archive @pieter-goosen Jan 9, 2022 at 5:55
2

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;
1
  • Its best to check this index exists but this work ;)
    – Sagive
    Jun 9, 2021 at 14:54
1

To summarize all the previous answers here is the helper gist I use. Does all the necessary checks and accepts same arguments as the get_the_terms() function.

function getTheFirstTerm($post, $taxonomy) {
    $post = get_post( $post );
    if ( ! $post ) {
        return false;
    }

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

    if (!empty($terms)  && !is_wp_error( $terms )) {
        //this line effectively returns the first available term object if there is one.
        return $terms[0];
    }

    return false;
}

Further note. This function for simplicity that's needed in most of my cases just returns the term object or false. You can still return WP_Error if up the flow you need check for it i.e.

if (!empty($terms)) {
   if(!is_wp_error( $terms )) {
       //this line effectively returns the first available term object if there is one.
       return $terms[0];
   } else {
       return $terms; //This returns the WP_Error object
   }       
}

Moreover you can sort the terms before returning the first i.e. so you get first term object by term_id, slug, name, menu order and so on.

2
  • 1
    Thanks for your answer j.w. ;)
    – Sagive
    Nov 11, 2021 at 0:55
  • You may point out in your code that which part give the first term so that other user may refer to it effectively. Nov 11, 2021 at 8:59
0

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;
    break;
    unset($term);
}
3
  • 1
    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, 2014 at 12:57
  • unset( $term ); should be after your foreach loop, and not inside it :-) Jul 28, 2015 at 8:18
  • This won't work if the returned terms are WP_Error.
    – J. Wrong
    Nov 11, 2021 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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