0

So I've created a custom post type (location) and a custom taxonomy (city) to go with it. I'm trying to set up a page where it lists the taxonomy terms followed by any posts associated with that term. But if there are no posts for that term, then nothing shows up.

I'm trying to do something like:

Term 1

  • post
  • post
  • post

Term 2

  • post
  • post

Term 3 (empty)

  • there are no posts for this term

How do I do that? Here is the code that I have (code updated to reflect recommendations from the comments below):

<?php
    $taxonomy       = 'city';
    $args           = array(
        'taxonomy' => $taxonomy,
        'hide_empty'     => false,
        'orderby'  => 'count',
        'order'    => 'DESC',
    );
    $taxonomy_terms = get_terms( $args );

    if ( $taxonomy_terms ) {
        foreach ( $taxonomy_terms as $taxonomy_term ) {

            $args = array(
                'post_type'      => 'location',
                'posts_per_page' => - 1,
                'hide_empty'     => false,
                'tax_query'      => array(
                    'relation' => 'AND',
                    'taxonomy' => $taxonomy,
                    'terms'    => $taxonomy_term->slug,
                    'field'    => 'slug',

                ),
            );

        $query = new WP_Query( $args );

        if ( $query->have_posts() ) : ?>
            <h4><?php echo $taxonomy_term->name; ?></h4>
            
            <div class="locations">
            
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>

                <div class="location">
                    <div class="price"><a href="<?php the_permalink(); ?>"><?php the_field('price_per_month'); ?> / month</a></div>
                    <a href="<?php the_permalink(); ?>">
                        <h3><?php the_field('street_address'); ?></h3>
                        <h4><?php the_field('city'); ?>, <?php the_field('state'); ?></h4>
                    </a>
                    <a href="<?php the_field('google_map_link'); ?>" rel="noreferrer" target="_blank"><i class="fal fa-map-marker-alt"></i> Google Maps</a>
                </div>
                
            <?php endwhile; ?>

            </div>

        <?php else : ?>

            <h4><?php echo $taxonomy_term->name; ?> (Coming Soon)</h4>

            <div class="locations">

                  <div class="location coming">
                      <h3></h3>
                      <h4></h4>
                      <a href="#" target="_blank"></a>
                  </div>

                  <div class="location coming">
                      <h3></h3>
                      <h4></h4>
                      <a href="#" target="_blank"></a>
                </div>

            </div>

        <?php wp_reset_postdata();
        endif;

    }
}
?>

Any help would be hugely appreciated!

1 Answer 1

0

As per the docs: https://developer.wordpress.org/reference/functions/get_terms/ the way to get terms has changed in WP 4.5. It's always good to keep the new way of dev.

Thats said, this code seems to work on my side:

    $taxonomy       = 'city';
    $args           = array(
        'taxonomy' => $taxonomy,
        'orderby'  => 'count',
        'order'    => 'DESC',
    );
    $taxonomy_terms = get_terms( $args );

    if ( $taxonomy_terms ) {
        foreach ( $taxonomy_terms as $taxonomy_term ) {

            $args = array(
                'post_type'      => 'location',
                'posts_per_page' => - 1,
                'hide_empty'     => false,
                'tax_query'      => array(
                    'relation' => 'AND',
                    'taxonomy' => $taxonomy,
                    'terms'    => $taxonomy_term->slug,
                    'field'    => 'slug',

                ),
            );

I usually use https://generatewp.com/wp_query/ to generate my queries.

4
  • I implemented it but it is only showing the city that has posts in it. I also need to be able to display the city name (term) when it is empty with some placeholder content. Feb 17 at 7:32
  • Try adding 'hide_empty' => false to the args for the first get_terms
    – majick
    Feb 17 at 11:52
  • It's getting closer to what I need - except now the same posts are showing up under each term name even though they are assigned to only one term (Houston): riggy.digitaldemo.net/locations Feb 17 at 13:48
  • Try moving wp_reset_postdata(); after endif !
    – majick
    Feb 18 at 11:40

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.