I have CPT "Dealers" and a custom tax of "States" where each post in the CPT is associated to one state. The hope is to create the header for the state then list each post in that state taxonomy under it; and only show the state header if at least one post is associated with it.
All this does right now is display the STATE as the
h3 class="state"tag and doesn't list the posts associated with the tax.
e.g.
STATE 1
STATE 2
It should be creating a div with the class of "dealer" for each post under the header of the state. e.g:
STATE 1
Post Title
Address
City, ST
800.555.1234
www.example.com
Post Title
Address
City, ST
800.555.1234
www.example.com
STATE 2
Post Title
Address
City, ST
800.555.1234
www.example.com
I'm using Advanced Custom Fields to call in the $street_address, etc. Here's the code I have right now.
Thoughts?
<?php
$states = get_terms( 'states' );
?>
<?php
foreach ( $states as $state ) {
$state_query = new WP_Query( array(
'post_type' => 'dealers',
'tax_query' => array(
array(
'taxonomy' => 'states',
'field' => 'name',
'terms' => $_GET['state'],
'operator' => 'IN'
)
)
) );
?>
<h3 class="state"><?php echo $state->name; ?></h3>
<?php
if ( $state_query->have_posts() ) : while ( $state_query->have_posts() ) : the_post();
$id = get_the_ID();
$street_address = get_field ('street_address', $id);
$dealer_city = get_field ('dealer_city');
$dealer_state = get_field ('dealer_state');
$phone_number = get_field ('phone_number');
$toll_free = get_field ('toll_free');
$dealer_website = get_field ('dealer_website');
?>
<div class="dealer">
<h4 class="dealer_name"><?php the_title(); ?></h4>
<p><?php echo $street_address; ?></p>
<p><?php echo $dealer_city; ?>, <?php echo $dealer_state; ?></p>
<?php if ( get_field('phone_number') ) { ?><p><?php echo $phone_number; ?></p><?php } ?>
</div>
<?php endwhile; endif; ?>
<?php
wp_reset_postdata(); ?>
<?php } ?>