Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Wondering if anyone can help.

I'm putting together a site which features music reviews, amongst other things. The set up is built on a custom post type and a number of associated taxonomies.

Post type:

  • my_albumreviews

Taxonomies:

  • artist
  • label
  • release date

On a page displaying an album review ( my_albumreviews post type ), under the main content I need a section which shows "Other reviews from this artist". I think I'm along the right lines with this but just can't get it to work.

$artistname = get_the_terms($post->ID, 'artist');
        $args = array(
        'post_type'=>'my_albumreviews',
        'tax_query'=>array(array('taxonomy'=>'artist',
                    'field'=>'id',
                    'terms'=> '$artistname'
                    ))
        );
$artist_reviews = new WP_query($args);

This is then followed by..

while ($artist_reviews->have_posts()) : $artist_reviews->the_post();

..which returns nothing to the page.

Any help would be much appreciated.

share|improve this question
    
What comes after while ($artist_reviews->have_posts()) : $artist_reviews->the_post();? –  Bainternet Apr 15 '11 at 8:22
    
I'll be running standard in-loop functions like the_title() to display key info from all other reviews by the current artist. –  user4687 Apr 15 '11 at 12:50
add comment

4 Answers

The get_the_terms() returns a list of term objects. So first of all, you need to treat the output as a list rather than a single element. The name you're looking for is stored in the name field.

$artistnames = get_the_terms($post->ID, 'artist');
if (!empty($artistnames)) : ?>
    foreach ($artistnames as $obj) {
         $name = $obj->name;
         // Do something with the name
    }

Maybe you should use a custom field instead of a taxonomy? My experience is that they work better for singletons.

You get the custom fields in the loop by get_post_meta():

<?php $meta_values = get_post_meta($post->ID, 'artist', true); ?> 


Hopefully this helps you somewhat!

share|improve this answer
    
Thanks for the quick reply, Frederik. Unfortunately that doesn't seem to be working. I've substituted $artistname for the newly stored $name in my tax query but it's still not pushing anything to the page when I call for the_title(). I'll only ever be pulling one result into $artistname -- does this help matters? And I'm keen to stay on Taxonomies as their storage will help reduce errors through spelling eg. Beatles / Beetles. Thanks again. –  user4687 Apr 15 '11 at 13:56
add comment

When using 'tax_query' with 'field'=>'id' then terms should be an array of term ids, so instead of using get_the_terms() use wp_get_object_terms() and set the fields to ids that way you will get an array with term ids so:

$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'ids');
$artistnames = wp_get_object_terms($post->ID, 'artist',$args);

and you should be set using your code.

share|improve this answer
add comment
$artists = get_the_terms($post->ID, 'artist');
foreach ($artists as $artist) $artids[] = $artist->term_id;
$args = array(
   'post_type'=>'my_albumreviews',
   'tax_query'=>array(
      array('taxonomy'=>'artist',
         'field'=>'id',
         'terms'=> $artids,
      )
   )
);
$artist_reviews = new WP_query($args);

get_the_terms returns an array of objects. So you have to loop through it and save the ID numbers into another array to pass to the tax_query.

Also '$artistname' is the string '$artistname'. If it's a variable, it shouldn't have single quotes around it.

share|improve this answer
add comment
up vote 0 down vote accepted

@Frederik, @Bainternet & Otto

Thanks for your help, guys. A thousand thanks for your help! The key lied in stripping the taxonomy's slug out of wp_get_post_terms().

Here's my final, fully functioning code:

$artist_taxo = wp_get_post_terms( $post->ID, 'artist' );  //Fetch terms from taxonomy 'artist'
$artistname = $artistslug[0]->slug;  //Pull slug from $artist_taxo into $artistname

$args = array(
    'post_type' => 'cdx_albumreviews',  //Specify custom post name
    'numberposts' => -1,  //Return all posts
    'artist' => $artistname //Taxo 'artist' is equal to $artistname
    );

    $artist_reviews = new wp_query($args);
    while ($artist_reviews->have_posts()) : $artist_reviews->the_post();
    the_title();
    endwhile;

A massive thanks.

Mathew

share|improve this answer
add comment

Your Answer

 
discard

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