I'm currently using term_exists( $term, $taxonomy );
in a conditional, but the problem is that it includes terms that are not being used (although they exist in the list of categories).
Is there something I can use besides term_exists
that will include categories only if they are in use? Or do I somehow need to filter to get the terms in use?
I'm filtering a real estate properties archive via a select element. Once each week, as open houses are designated, I apply the 'Open House' category to them, allowing visitors to sort to see all open houses. After the weekend, I deleted the open house category from each listing. Example: https://megangulick.com/agent-listings/ (currently just editing the option by hand, so 'Open House does not show as an option).
What I'm looking to do is change the initial select option based on whether or not there are open houses currently designated.
Here's what my existing code looks like:
<?php $categories = get_categories('taxonomy=listing-neighborhood&post_type=listings'); ?>
<select name="listingcat" id="listingcat" onchange="submit()">
<?php //this is the bit I need a solution for
$cat = (term_exists('Open House', $categories));
if $cat {
echo '<option value="">Select neighborhood or open house</option>';
} else {
echo '<option value="">Select neighborhood</option>';
}
?>
<?php
foreach ($categories as $category) :
echo '<option value="'.$category->name.'"';
if(isset($_GET['listingcat']) && ($_GET['listingcat'] == $category->name)) {
echo ' selected="selected"';
}
echo '>'.$category->name.'</option>';
endforeach;
?>
</select>
The task here is how to determine if a particular term (ie, Open House) is in use, then use the information for an if/else conditional statement.
has_term()
-- something likehas_term( 'Open House', 'listing-category', get_the_ID() )
might be what you need instead of yourterm_exists()
call.