1

Having formulated my $query for a custom taxonomy on a page template, how would I ask if a specific term has posts?

$args = array(
    'post_type' => 'exhibitions',
    'tax_query' => array(
        array(
            'taxonomy' => 'exhibition',
            'field'    => 'slug'
        ),
    )
);
$query = new WP_Query($args);

Assuming I'm on the right track, a verbal description of the sort of conditional statements I'm looking for would be:

if the $query taxonomy term 'current' have posts, do something;

elseif the $query taxonomy term 'upcoming' have posts, do something else;

2 Answers 2

0

I'm not sure what you exactly need, but normally, by default, get_terms returns only terms that actually have posts assigned to them

$terms = get_terms( 'exhibition' );
var_dump( $terms );

Apart from this, I really do not know what you exactly need

3
  • That sounds like it could work. Just for the record, my usage scenario is not ideal. The custom theme I inherited from the previous dev needs to be re-coded so this is part of a temporary workaround: a page which redirects to the correct template with header("Location: ".$exhb_link); depending on which term has posts. First 'Current'. If not, 'Upcoming'. If not, 'Past'. Oct 21, 2015 at 14:36
  • It does look quite disjointed, and it confuses me big time. Sorry that I can not help you out. Oct 21, 2015 at 14:46
  • I think you did help me, Pieter. I'll test it and mark the working solution as answered. Oct 21, 2015 at 14:48
2

It sounds like you want has_term(). Something like: Feed your query an array of terms:

$args = array(
    'post_type' => 'exhibitions',
    'tax_query' => array(
        array(
            'taxonomy' => 'exhibition',
            'field'    => 'slug',
            'terms' => array(
              'current',
              'upcoming',
            ),
        ),
    )
);
$query = new WP_Query($args);

Then loop over it multiple times:

if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    if (has_term('current','exhibition')) {
      // stuff
    }
  }
}
$query->rewind_posts();
if ($query->have_posts()) {
  while ($query->have_posts()) {
    $query->the_post();
    if (has_term('upcoming','exhibition')) {
      // stuff
    }
  }
}
$query->rewind_posts();
4
  • Thanks, @s_ha_dum, but not quite. This is more like the reverse of checking if a post has terms. I need to check if a query's particular term has posts. I could do it by re-formulating the entire query several times, each time with tax_query ... 'terms' => 'current' then again with 'terms' => 'upcoming' etc. but that seems excessive. Oct 21, 2015 at 13:49
  • @simbasounds : see the edit
    – s_ha_dum
    Oct 21, 2015 at 14:14
  • Thanks, good to know. I'll try the get_terms method first since it seems like it'll work for my requirements which are very basic. Oct 21, 2015 at 14:47
  • has_term is what I was looking for as well, thank you, good answer.
    – lowtechsun
    Jan 13, 2017 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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