0

I have a custom post type that I created to list race results. The main archive can be found at: https://www.bowlertransmissions.com/mmc/

Clicking on the results for '2016' brings you to: https://www.bowlertransmissions.com/mmc/2016/

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.

---UPDATE---

I have managed to solve this problem with what I consider to be a very inelegant solution. I essentially used the query for the current page to test for existing 'event-name' taxonomy terms, then added them to an array if they weren't already in the array. I feel like there has to be a better way to do this, but I have not found it.

<?php
$eventNames = array();
while( $the_query->have_posts() ) : $the_query->the_post();
    $testTerm = iw_get_tax_term('event-name');
    if ( !in_array( $testTerm, $eventNames ) ) {
        array_push($eventNames, $testTerm);
    }
endwhile;
foreach ($eventNames as $key => $value) {
    echo '<a class="x-btn mmc-link-button btn-' . $value . '" href="' . home_url( $wp->request ) . '">' . $value . '</a>';
}
?>

If anyone knows of a better way, please let me know.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.