This might be complicated to explain and I don’t know if there is a solution to that!?
I have a custom-post-type named “wr_event” and a hierarchical custom-taxonomy named “event_type”. The custom-post-type has a meta-box for event_date
and all posts associated with this post-type are sorted by this event_date
. I have one special condition in my loop to query if the event_date
has already taken place – in that case it’s not shown but only listed in my archives.
Just like you can use wp_list_categories()
I wrote a custom function that lists all taxonomy-terms exactly the same way.
And now my question:
When I use my custom wr_list_taxonomy()
function it lists all taxonomies that have a post associated with it. Just like wp_list_categories()
lists only the categories that have a post associated with it. If there would be a taxonomy-term that doesn’t have posts it wouldn’t be listed.
I wonder if it’s somehow possible to “exclude” a taxonomy-term or simply not list it if all posts associated with it have an event_date
that has already taken place.
Sounds complicated right?
So I have this function that works like a charm.
function wr_list_taxonomy($taxonomy, $orderby, $hierarchical) {
$show_count = 0;
$pad_counts = 0;
$title = '';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
return wp_list_categories( $args );
}
It lists all terms and only lists them if posts are in them. However I have posts (“events” in my case) in all taxonomy-terms, but have terms whose events are all over.
And if the “events” in a term are all over (and therefore the term-page doesn’t list anything as I wrote above because I filter them by todays date) I don’t want this function wr_list_taxonomy()
to print this term.
Is that somehow possible?
It sounds like you’re looking for
wp_get_object_terms()
which takes an array of post IDs, a taxonomy, and some optional arguments to determine what to return.So, you’d use
get_posts()
to query future events, build an array of IDs, and then pass that array towp_get_object_terms()
.