Get terms from current WP Query

I have a custom post type that I created to list race results. The main archive can be found at:

Clicking on the results for ‘2016’ brings you to:

From this page I want to send people to the next taxonomy archive, which is the ‘event-name’ taxonomy. There are four possible choices (autocross, shootout, hot-lap, top-speed), but I only want to show the option if that taxonomy has any posts within the current year. For instance, adding ‘hot-lap/’ to the link above shouldn’t have any posts. It currently shows results for 2015, but that is another issue I need to sort out.

I want to get the taxonomy terms of the ‘event-name’ taxonomy that also have the taxonomy term of 2016 for the ‘event-year’ taxonomy. So really I should only get the terms ‘autocross’ and ‘shootout’ when I am on the 2016 ‘event-year’ archive. I can of course hard code it, but that would require a change if results for the taxonomy term ‘top-speed’ are added to the ‘event-name’ taxonomy under 2016.

I have tried:

$terms = get_terms('event-name');
foreach ( $terms as $term ) {
    echo '<li>' . $term->name . '&nbsp;(' . $term->count . ')' . '</li>';

But this returns all of the ‘event-name’ terms and not just the ones that have posts with the ‘2016’ term in the ‘event-year’ taxonomy. I have tried many other things, but I either get no terms or all possible terms.

Any help would be greatly appreciated.

Read more here: Get terms from current WP Query

Leave a Reply

Your email address will not be published. Required fields are marked *