2

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.

Schwoebel
  • 216
  • 1
  • 4
  • 19

4 Answers4

1

wp_get_post_terms only returns terms that have been selected for that post, it doesn't return all taxonomy terms.

http://codex.wordpress.org/Function_Reference/wp_get_post_terms

Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • I must be going astray somewhere then. Let me ask you this, and I'm sorry if I'm sound ridiculous. after getting wp_get_post_terms()'s array from my specific post and taxonomy when I do $thevariableholdingthearray->name; nothing will echo from that. I have also tried $thevariableholdingthearray['name'] and $thevariableholdingthearray[1]. I am frustrated :-( – Schwoebel Jul 29 '11 at 09:11
  • Try `foreach($array as $term) { echo $term->slug; }` – Dogbert Jul 29 '11 at 09:19
  • Son of a glitch. You know what, I was using the_ID() to return the ID of the post I wanted. I should have been using get_the_ID();. Thanks for you time Dogbert. – Schwoebel Jul 29 '11 at 10:39
0
<?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> ';
} 
?>
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Sam Adah
  • 3
  • 5
0

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>';
?>
0
<?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) ,''; ?>
Nickfmc
  • 369
  • 1
  • 8