admin管理员组文章数量:1123935
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: / (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.
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.
Share Improve this question edited Mar 19, 2024 at 23:35 Ray Gulick asked Mar 18, 2024 at 18:39 Ray GulickRay Gulick 5266 silver badges20 bronze badges 6 | Show 1 more comment2 Answers
Reset to default 1Started researching term arrays and came up with something that works, the solution for creating a conditional based on whether or not a term is in use by any post:
<?php $categories = get_categories('taxonomy=listing-neighborhood&post_type=listings'); ?>
<?php
$term_name = wp_list_pluck( $categories, 'name' );
$this_name= 'Open House';
if( in_array( $this_name, $term_name ) ){
echo '<option value="">Select status or open house</option>';
} else {
echo '<option value="">Select status (show all by default)</option>';
}
?>
I appreciate everyone's help. I think my initial question was misleading, not the question I needed to ask to find the solution.
To retrieve only the terms that are in use associate posts you can use the get_terms()
function with the pad_counts
parameter set to true. This will ensure that the count property of each term object reflects the number of associated posts. You can then filter out terms with a count of 0
$taxonomy = 'your_taxonomy'; // Replace 'your_taxonomy' with the name of your taxonomy
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'pad_counts' => true, // Set to true to include the count of associated posts
));
// Filter terms to include only those with associated posts
$used_terms = array_filter($terms, function($term) {
return $term->count > 0;
});
// Check if any terms are retrieved
if (!empty($used_terms)) {
foreach ($used_terms as $term) {
// Output term information
echo '<p>Term Name: ' . $term->name . '</p>';
echo '<p>Term ID: ' . $term->term_id . '</p>';
echo '<p>Term Slug: ' . $term->slug . '</p>';
echo '<p>Term Description: ' . $term->description . '</p>';
echo '<p>Post Count: ' . $term->count . '</p>';
// You can output more information about the term if needed
}
} else {
echo 'No terms found with associated posts.';
}
本文标签: Retrieve only terms that are in use
版权声明:本文标题:Retrieve only terms that are in use 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736608491a1945384.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
has_term()
-- something likehas_term( 'Open House', 'listing-category', get_the_ID() )
might be what you need instead of yourterm_exists()
call. – Pat J Commented Mar 20, 2024 at 2:48