Get the ‘slug’ of a custom taxonomy

Use case:

I have a custom post type of ‘show’ for a music venue. In the sidebar we want to display any related ‘show’ to the current one on single-show.php. They will be related by a custom taxonomy of ‘genre’. I figured I could dump the custom taxonomy slugs for a post (might be more than one) into a variable then pass that variable into a custom query for the sidebar post.

Read More

Using get_the_term_list() works if there is a single taxonomy but if there are multiple it breaks (of course).

Any thoughts on how to get an array of the custom taxonomy slugs for a give post into a variable?

Related posts

Leave a Reply

1 comment

  1. You can do something like the following:

    $terms = get_the_terms( $post->id, 'genre' ); // get an array of all the terms as objects.
    
    $terms_slugs = array();
    
    foreach( $terms as $term ) {
        $terms_slugs[] = $term->slug; // save the slugs in an array
    }