I am trying to get the following:
State
-Healthcare (Category)
--City
---Property 1
---Property 2
I have my query working to get:
State
-Healthcare (Category)
--Property 1
--Property 2
And I have no idea how to get City as a subhead!
This is my code:
<h1 style="padding-bottom: 30px; font-weight: 600;">South Carolina</h1>
<?php //start by fetching the terms for the animal_cat taxonomy
$terms = get_terms( 'property-category', array(
'orderby' => 'count',
'hide_empty' => 0
) );
// now run a query for each animal family
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'property',
'property-category' => $term->slug,
'meta_query' => array(
array (
'key' => 'state',
'value' => 'SC'
),
array(
'key' => 'available',
'compare' => '=',
'value' => '1'
)
)
);
$query = null;
$query = new WP_Query( $args );
// output the category name in a heading tag
if( $query->have_posts() ) :
echo'<div class="collapsible"> ' . $term->name . ''; echo' <span style="font-size:12px; vertical-align: middle;">(' . $query->post_count . ')</span></div>';
// output the post titles in a list
echo '<ul class="content">';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post(); ?>
<li style="font-size: 12px; list-style: none; text-transform: none; line-height: 30px; padding-left: 15px;" id="post-<?php the_ID(); ?>">
<?php the_field('town'); ?>
// ****THIS IS WHERE I NEED TO SHOW PROPERTIES UNDER CITY???
<ul>
<li style="font-size: 12px; list-style: none; text-transform: none; line-height: 30px; padding-left: 15px;" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
</ul>
</li>
<?php
endwhile;
endif;
echo '</ul>';
// use reset postdata to restore orginal query
wp_reset_postdata();
} ?>
I am totally stuck, do I need to setup another query?
EDIT
I have this now, and it is showing the city/town above each list of properties available in that city/town, but it duplicates the city/town header like this:
Heathcare
Salisbury
--Medical Office A
Charlotte
--Medical Office D
Salisbury
--Medical Office B
Charlotte
--Medical Office C
This is how I want it to show:
Heathcare
Salisbury
--Medical Office A
--Medical Office B
Charlotte
--Medical Office D
--Medical Office C
This is my code:
<div id="sidebar1">
<h1 style="padding-bottom: 30px; font-weight: 600; padding-left: 50px;">North Carolina</h1>
<?php //start by fetching the terms for the animal_cat taxonomy
$terms = get_terms( 'property-category', array(
'orderby' => 'count',
'hide_empty' => 0
) );
// now run a query for each animal family
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'property',
'property-category' => $term->slug,
'meta_query' => array(
array (
'key' => 'state',
'value' => 'NC'
),
array(
'key' => 'available',
'compare' => '=',
'value' => '1'
)
)
);
$query = null;
$query = new WP_Query( $args );
// output the category name in a heading tag
if( $query->have_posts() ) :
echo'<div class="collapsible G"> ' . $term->name . ''; echo' <span style="font-size:12px; vertical-align: middle;">(' . $query->post_count . ')</span></div>';
// output the post titles in a list
echo '<ul class="content">';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post(); ?><?php endwhile;
echo '<ul style="margin-left: 0;">';
// Start second Loop
$args = array(
'post_type' => 'property',
'meta_query' => array (
array (
'key' => 'state',
'value' => 'NC',
),
array(
'key' => 'town',
)
)
);
while ( $query->have_posts() ) : $query->the_post(); ?>
<div><?php the_field('town'); ?></div>
<li class="city" id="post-<?php the_ID(); ?>">
<li style="font-size: 15px; list-style: none; text-transform: none; line-height: 30px;" id="post-<?php the_ID(); ?>">
<a class="prop" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <i class="fas fa-angle-right"></i>
</li>
<?php
endwhile;
endif;
echo '</ul></li></ul>';
// use reset postdata to restore orginal query
wp_reset_postdata();
} ?>
</div>
What am I doing wrong?