0

I'm trying to do a query for a post with three terms in a custom taxonomy. Then, in the loop, I want to output just the posts with one of these terms:

$args = array(
  'post_status'    => array( 'publish' ),
  'tax_query'      => array(
    array(
        'taxonomy' => 'prr',
        'field'    => 'slug',
        'terms'    => ['proteger', 'respetar', 'remediar'],
    ),
  )
);
$prr_query = new WP_Query( $args ); ?>
if ( $prr_query->have_posts() ) {
    while ( $prr_query->have_posts() ) {
        $prr_query->the_post();
        if (has_term('proteger')) {
          the_title();
        }
    }
}
wp_reset_postdata();

It does not work.

If I do a loop without conditional, it works:

if ( $prr_query->have_posts() ) {
    while ( $prr_query->have_posts() ) {
        $prr_query->the_post();
          the_title();
    }
}
wp_reset_postdata();

If I do a var_dump($prr_query) I get this:

https://pastebin.com/jZq5t8Bt

I can see, in this query, that post objects do not includes term list.

How can I output just the posts that has a specific term?

1

Since you're dealing with custom taxonomies, you must mention that custom taxonomy in has_term().

if ( $prr_query->have_posts() ) {
    while ( $prr_query->have_posts() ) {
        $prr_query->the_post();
        if (has_term('proteger', 'prr')) {
          the_title();
        }
    }
}

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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