Only displaying a posts selected taxonomy terms?

When using wp_get_post_terms() I can produce a list of taxonomy terms associated with a post. However, I only want to show the taxonomy terms that have been selected for that post. Using the aforementioned function and get_terms() will successfully find the taxonomy terms, but it will show all of the terms. Not only the ones that have been selected. In the $args array for the functions I’ve looked for a ‘selected’ filter, but I found none and when I tried it, it didn’t work.

Am I trying to do something that can’t be done? I’m sure it’s something that is starring me right in the face. I just want to ask the pro’s before I make major changes to the way I’m doing things.

Related posts

Leave a Reply

4 comments

  1. <?php
    $the_selected = $_GET['cat'];
    $args = array( 'post_type' => 'portfolio_item', 'posts_per_page' => 11, 'orderby' => 'id', 'order' => 'DESC', 'themes_categories' => "$the_selected");
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>
    

    This works well for me. I simply send the taxonomy slug to browser, and itterate through them with the code above.

    I send by this:

    <li>Filter By:</li>
    <?php
    $categories=get_categories($args);
      foreach($categories as $category) { 
        echo '<li><a href="' . get_category_link( $category->term_id ) . '?cat=' . $category->slug.'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </li> ';
    } 
    ?>
    
  2. You can try out this code, it worked for me. I have a taxonomy named ‘stores’ and i wanted to display 2 selected taxonomy from it. so i used a include feature.

    <?php
    $taxonomy = 'stores';
    $args1=array(
        'include'=> array(12,30)
        );
    
    $terms = get_terms('stores',$args1 );
    echo '<ul>';
    
    
    foreach ($terms as $term) {
        //Always check if it's an error before continuing. get_term_link() can be finicky sometimes
        $term_link = get_term_link( $term, 'stores' );
        if( is_wp_error( $term_link ) )
            continue;
        //We successfully got a link. Print it out.
    
    
        echo '<li><a href="' . $term_link . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';
    ?>
    
  3. <?php echo get_the_term_list( $post->ID, 'your_taxonamy'); ?>
    

    And if you want it without the term linking you can use this

    <?php $terms_as_text = get_the_term_list( $post->ID,'your_taxonamy'); if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) ,''; ?>