1

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.

6
  • Have you tried adding &hide_empty=1 in your query string?
    – birgire
    Commented Mar 19 at 17:10
  • 1
    Is this what you mean? <?php $categories = get_categories('taxonomy=listing-category&post_type=listings&hide_empty=1'); ?> Doesn't have any effect.
    – Ray Gulick
    Commented Mar 19 at 17:24
  • It should hide empty ones by default though, I guess Im misunderstanding the setup and the question.
    – birgire
    Commented Mar 19 at 18:03
  • The dropdown displays terms perfectly, including only showing the ones that are in use. What i'm trying to do is show a different first option depending on if 'Open House' is in use. I suspect 'term exists' is the wrong function to use, as it gets all the terms, in use or not. IF 'Open House' is among active terms THEN SHOW <option value="">Select neighborhood or open house</option> ELSE SHOW <option value="">Select neighborhood</option>
    – Ray Gulick
    Commented Mar 19 at 21:29
  • If you're trying to determine if a given post has a given term, you can use has_term() -- something like has_term( 'Open House', 'listing-category', get_the_ID() ) might be what you need instead of your term_exists() call.
    – Pat J
    Commented Mar 20 at 2:48

2 Answers 2

1

Started 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.

-1

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.';
}
2
  • That's going to require a bigger rewrite than I was hoping for, but it looks promising. I'll get back with results later this morning. Thanks.
    – Ray Gulick
    Commented Mar 19 at 10:46
  • This doesn't really work in my instance (addng my existing code and further info in my original question).
    – Ray Gulick
    Commented Mar 19 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.