WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need the terms in my tax_query to be the ones from the current post. I am trying to this by using a variable. The value of the variable is given by using the get_terms() function.

The problem is that it is not returning anything. It does however return the correct posts when I manually put the terms in the array.

How do I grab the posts terms automatically and place them in the terms parameter?

Here is the code that I am using.

//for use in the loop, list 5 post titles related to first tag on current post
$tags1 = get_terms($post->ID, 'b');
if ($tags1) {
echo '<div id="productsideheaders"><h2>PRODUCTS</h2></div>';
$args = array(
'tax_query' => array(
'taxonomy' => 'b',
'terms' => array('$tags1'),
'field' => 'slug',

$query = new WP_Query( $args );
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div style="width: 270px;float: left;">
<?php if ( has_post_thumbnail()) : ?>
title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail( array(40,40) ); ?>
<span style="font-size: 10pt;">" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title('' ); ?></span></div>
<?php endif; ?>

share|improve this question

'terms' => array('$tags1'),

Remove the single quotes and it should look like 'terms' => array($tags1) OR 'terms' => $tags1

No need to make it an array, if you are using just one value.

share|improve this answer
$tags1 = wp_get_post_terms($post->ID, 'b');

Is what you are looking for? It gets sort of complicated with taxonomies and WordPress

get_terms doesn't work by post_id, it only lists categories.

share|improve this answer
The problem seems to be with the variable in the terms => $tags1. The get_terms(post->ID, 'b'); is getting the right terms but I can't get the variable assigned to them to work in the terms parameter. – Garret Sep 12 '12 at 0:46

Your Answer


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

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