简体   繁体   中英

WP_Query posts get term list outside of loop?

Maybe I'm going about this incorrectly but I'm having an issue with getting info outside of the while loop:

 <?php $title = get_field('car_list_title'); $field = get_field('tax_field_selector'); $query = new WP_Query( array( 'post_type' => 'cars', 'taxonomy' =>'make', 'term' => $field->name, 'posts_per_page' => -1, 'orderby' =>'title', 'order' =>'ASC' ) ); $taxonomy = get_terms( array( 'taxonomy' => 'location', 'hide_empty' => true ) ); if ( $field || $query->have_posts() ) : ?> <div class="c-cars"> <h2 class="c-cars_title ut--underline--lightblue"> <?= $title; ?> </h2> <?php foreach( $taxonomy as $tax ) : $tax_name = $tax->name; ?> <div class="c-cars_row"> <h4 class="c-cars_location-title"> <?= $tax_name; ?> </h4> <div class="c-cars_cars"> <?php while ( $query->have_posts() ) : $query->the_post(); $title = get_the_title(); $link = get_permalink(); $image = get_field('car-picture'); $image_alt = get_field('car_alt'); $image_title = get_field('car_title'); $post_id = get_the_ID(); $terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true)); $location = $terms[0]->name; ?> <?php if( $location === $tax_name ) : ?> <div class="c-cars_car"> <a href="<?= $link; ?>"> <img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>"> </a> <h4 class="text-center"> <a href="<?= $link; ?>"> <?= $title; ?> </a> </h4> </div> <?php endif; ?> <?php endwhile; wp_reset_postdata(); ?> </div> </div> <?php endforeach; ?> </div> <?php endif; ?> 

So what happens here is I get a list of the locations and all the cars in those locations:

Location 1:

  • Car
  • Car
  • Car

Location 2:

  • Car
  • Car
  • Car

Location 3:

Location 4:

  • Car
  • Car
  • Car

The problem here is, as an example, Location 3 shows up even though there's no "posts" in that term.

The while loop is only cars of a specific model, sorted into what location they are at.

I'm not really sure how to filter out the empty locations.

I do:

<?php if( $location === $tax_name ) : ?>

Inside of the loop and that filters them out of the locations but still leaves the location title because it's outside of the while loop. If I were able to do this earlier up in the code it may work but I can't get the list of active terms outside of the while loop.

I'm kind of lost right now. Any ideas or suggestions? Thanks!

you can check using condition if has a term so show title either it will be blank try it below condition and mentioned in a comment if it's work or not.

has_terms https://developer.wordpress.org/reference/functions/has_term/

function check if the post has terms.

if( has_term( $location, $tax_name ) ) {
// do something
}

Okay, I have updated your answer please try it below code. I have just get post count of terms and applied condition if terms has post count so show terms title name or if there is no post count of terms so the title will show blank.

 <?php $title = get_field('car_list_title'); $field = get_field('tax_field_selector'); $query = new WP_Query( array( 'post_type' => 'cars', 'taxonomy' =>'make', 'term' => $field->name, 'posts_per_page' => -1, 'orderby' =>'title', 'order' =>'ASC' ) ); $taxonomy = get_terms( array( 'taxonomy' => 'location', 'hide_empty' => true ) ); if ( $field || $query->have_posts() ) : ?> <div class="c-cars"> <h2 class="c-cars_title ut--underline--lightblue"> <?= $title; ?> </h2> <?php foreach( $taxonomy as $tax ) : $tax_name = $tax->name; $tax_post_count = $tax->count; ?> <div class="c-cars_row"> if ( $tax_post_count > 0 ) : ?> <h4 class="c-cars_location-title"> <?= $tax_name; ?> </h4> <?php else : ?> <h4 class="c-cars_location-title"> <?= $tax_name = ''; ?> </h4> <?php endif; ?> <div class="c-cars_cars"> <?php while ( $query->have_posts() ) : $query->the_post(); $title = get_the_title(); $link = get_permalink(); $image = get_field('car-picture'); $image_alt = get_field('car_alt'); $image_title = get_field('car_title'); $post_id = get_the_ID(); $terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true)); $location = $terms[0]->name; ?> <?php if( $location === $tax_name ) : ?> <div class="c-cars_car"> <a href="<?= $link; ?>"> <img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>"> </a> <h4 class="text-center"> <a href="<?= $link; ?>"> <?= $title; ?> </a> </h4> </div> <?php endif; ?> <?php endwhile; wp_reset_postdata(); ?> </div> </div> <?php endforeach; ?> </div> <?php endif; ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM