• organu

    (@organu)


    Hello
    What I’m trying to achieve is a list with all event categories that have future events. Now i’m using this code

    $args = array(
    'type' => 'event',
    'hide_empty' => true,
    'taxonomy' => 'event-categories',
    'orderby' => 'id',
    'pad_counts' => false 
    ); 
    $eventcats = get_categories( $args );
    foreach ($eventcats as $eventcat) {
        echo '<div class="filter" data-filter=".'.$eventcat->category_nicename.'">'.$eventcat->cat_name.'</div>';
    }

    and still shows the categories with past events, because they aren’t empty.
    Any ideas?

    • This topic was modified 7 years ago by organu.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter organu

    (@organu)

    So found this.

    First i did a custom query for all the future published events

    $today = date('Y-m-d');
    $argp = array(
    'post_type' => 'event', 
    'posts_per_page'=>'-1',
    'post_status' => 'publish',
    'meta_query' => array(
        array(
        'key' => '_event_start_date',
        'value' => $today,
        'compare' => '>=',
    )
    ),
    'meta_key' => '_event_start_date', 
    'orderby' => 'meta_value_num', 
    'order' => 'ASC', 
    );
    $loopev = new WP_Query( $argp );

    Then loop posts, get post category id, make an unique array from categories ids

    if($loopev->have_posts()){ 
    $catf = array();
    while ( $loopev->have_posts() ) : $loopev->the_post(); 			
        $postId = get_the_id();
        $catids = get_the_terms( $postId, 'event-categories' );
        foreach( $catids as $catid ){
            $catf[] = $catid->term_id;
        }
    endwhile;
    }
    $filtered = array_unique($catf);

    After that get categories with include

    $args = array(
    'type' => 'event',
    'taxonomy' => 'event-categories',
        'include' => $filtered,
        'orderby' => 'id',
    'pad_counts' => true 
    ); 
    $categoriile = get_categories( $args );

    I don’t know if was the right way to do it, but it works for me.

    I think what was missing from the original code was this:

    'scope' => 'future'

    Thread Starter organu

    (@organu)

    Adding ‘scope’ => ‘future’ to args not solving the problem. I don’t know if wp_query, get_terms, and get_categories know how to interpret scope

    <?php
    	$args = array(
    	'type' => 'event',
    	'scope' => 'future',
    	'hide_empty' => true,
    	'taxonomy' => 'event-categories',
    	'pad_counts' => false 
    	); 
    	$categoriile = get_categories( $args );
    	foreach ($categoriile as $categoria) {
    		echo '<div class="filter" data-filter=".'.$categoria->category_nicename.'"><div >'.$categoria->cat_name.'</div></div>'			;
     	}
    	?>
    Plugin Support angelo_nwl

    (@angelo_nwl)

    maybe this snippet can help you out

    https://pastebin.com/f6DVrzZS
    https://pastebin.com/YHUAngC0

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘List all event categories that have future events’ is closed to new replies.