WP_query taxonomy issue

I don’t know if you can help me with issues that aren’t to do with your plugins or themes, but from what I read on your website you’ll help with pretty much anything WP related.

I have what’s probably an incredibly simple issue, however I’m having trouble passing parameters through my query. I’m basically trying to pass the terms as a variable to the tax query however it doesn’t work, if i manually write in the terms, it works fine, I don’t understand why the variable doesn’t, because if I echo the variable into the page, it outputs exactly what i type in manually.

Are you able to assist with this? If so my query is below:

<?php

$terms = the_field('product_groups');
echo $terms;

$args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'order_by' => 'date', // it's also default
'order' => 'DESC', // it's also default
'tax_query' => array(
array(
'taxonomy' => 'product-group',
'terms' => array( $terms )
)
)
);

// the query
$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->

<?php wp_reset_postdata(); ?>

<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Like i said, if i echo $terms into the page, it gives me ’10, 12, 11′, if i replace the variable in the query with that, it works fine, but with the variable, nothing.

  • Orbital
    • WPMU DEV Initiate

    Hi Jude,

    wasn’t quite it but almost, used you’re line and then had to change the top line which was $terms = the_field('product_groups');

    to the following:

    $terms = get_field('product_groups');

    and this now works, thank you so much for your fast and great response, i knew i was missing something stupid.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.