0

I'm looking for a way to filter the results of a for each loop based on a custom taxonomy when the WP_Query inside the for each loop has no results.

More specific, I have an Event post type with a custom taxonomy. On a page I want to display all taxonomies but only if that taxonomy has events where the event data is in the future. The event date is a custom field (ACF).

Th following code shows all taxonomies even when the WP_Query has no results / when there are now events for that taxonomy in the future.

<?php // Get all the categories
$categories = get_terms( 'soort-event');

foreach ( $categories as $category ):

// Chech if event start date is in the future  
$meta_query = array(
            array(
                'key'     => 'datum_event',
                'value'   => date('Ymd'),
                'type'    => 'DATE',
                'compare' => '>',
                )
            ); 
// Get all events
$events = new WP_Query(
    array(
            'post_type'       => 'events',
            'orderby'         => 'meta_value_num',
            'order'           => 'ASC',
            'posts_per_page'  => '3',  
            'tax_query' => array(
            array(
                'taxonomy'  => 'soort-event',
                'terms'     =>  $category,
                'field'     => 'term_id'
            )
        ),             
            'meta_query'      => $meta_query
      )
 );

if($events -> have_posts()):?> 
?>
 
<section class="events">      

<h2><?php // Show the event taxonomy name
   echo $category->name;?></h2>

<div class="row events">

    <?php // Show the event details
    while ($events->have_posts()) : $events->the_post(); ?>
         
    <div class="event-meta">
 
        <?php the_title();?>

    </div>

    <?php // End  event loop 
    endwhile;?> 

</section>

<?php
// end Events loop
endif;
// end for each taxonomy loop
endforeach;
wp_reset_postdata();
?>

Is there a way to filter the results and hide the 'empty' taxonomies which don't have an Event where the event date is in the future?

Update: code updated and added if statement to check for posts.

3
  • I notice your post loop is incomplete and has had the standard if check removed, and assumes that posts are always found. What is the reason for this? All post loops should have an if ( have_posts() ) type check, after all how else would you print out that nothing was found? But this code has removed it. Also, please indent your code correctly, it's difficult to read
    – Tom J Nowell
    Commented Mar 22, 2021 at 12:26
  • There's no specific reason for this, just overlooked it and once added it solved the issue. See the updated question. So thanks for commenting and checking the code!
    – NielsPilon
    Commented Mar 22, 2021 at 12:48
  • If that solves it can you post it as the solution below?
    – Tom J Nowell
    Commented Mar 22, 2021 at 12:55

1 Answer 1

0

Adding a if (has_posts() ) solved the issue which I forget to add. See the updated code in the question.

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.