Support

Account

Home Forums General Issues How to query event post type after current date with multiple taxonomies

Helping

How to query event post type after current date with multiple taxonomies

  • I’ve created an Events post type that is categorized using two taxonomies: Event Type and Academic Program

    I’m able to create list of Event posts and include a filter form to pull up specific events categories based on what’s selected in two drop down selections.

    I want to filter the page by checking the current date and only posting events greater than the event_starting_date which is a datepicker field.

    The first section is working code that allows the filtering dropdown:

    <?php
    $args = array(
    'taxonomy' => 'academic_programs',
    'show_option_all' => '- Any -',
    'show_count' => 0,
    'name'  => 'academic_programs',
    'orderby' => 'name',
    'value_field' => 'slug',
    'echo' => 0
    );
    $select = wp_dropdown_categories( $args );
    $select = preg_replace("#<select([^>]*)>#", "<select$1> ", $select);
    echo $select;
    ?>
    
    <?php
    $args = array(
    'taxonomy' => 'event_type',
    'show_option_all' => '- Any -',
    'show_count' => 0,
    'name'  => 'event_type',
    'orderby' => 'name',
    'value_field' => 'slug',
    'echo' => 0
    );
    $select = wp_dropdown_categories( $args );
    $select = preg_replace("#<select([^>]*)>#", "<select$1> ", $select);
    echo $select;
    ?>

    This is where I believe I have an issue:

    <?php
        $queried_object = get_queried_object () ;
        $today = date('Ymd');
        $args = array (
            'category_name'  => 'events',
             'posts_per_page' => 6,
             'orderby'   => 'meta_value',
             'meta_key' => 'event_start_date',
             'order' => 'ASC',
             'meta_query' => array(
                array(
                    'key' => 'event_start_date',
                    'meta-value' => $value,
                    'value' => $today,
                    'type' => 'DATE',
                    'compare' => '>=')),
        ) ;
        $query = new WP_Query ($args) ;
        ?>

    Everything past the orderby argument down to the meta query array is where things seem to break.

    This is how I get the query to post. If I leave out the above code block and remove the $query variable, I do get the filter to work, albeit without the necessary sort.

    <?php if ( $query->have_posts() ) {  while ( $query->have_posts() ) : $query->the_post(); ?>
                            <span><?php $date = get_field('event_start_date');
                            $starttime = get_field('event_start_time');
                            echo $date . " - " . $starttime; ?></span></br> 
                         <?php endwhile; } ?>

    Any help would be appreciated.

  • Hmm,

    If events is the custom post type, then this isn’t right:
    'category_name' => 'events',

    Your meta query seems to have 2 values you’re passing in:
    'meta-value' => $value,
    'value' => $today,

    What if you combine everything into one query, something like:

    
    $args = array(
    	'post_type'			=> 'event_cpt',			
    	'posts_per_page'	=> 6,
    	
        'orderby'    => 'meta_value_num',
        'order'      => 'ASC',
        'meta_query' => array(
            array(
                'key'     => 'event_start_date',
                'value'   => $today,
    			'type' 	  => 'DATE',
                'compare' => '>=',
            ),
        ),
              	
    );
    
    $args['meta_query'] = array( 'relation' => 'AND' );	
    
    $args['tax_query'] = array(
    	'relation' 		=> 'OR', #AND
    
    	array(
    		'taxonomy'	=> 'academic_programs',
    		'field'		=> 'term_id',
    		'terms'		=> $academic_programs_id
    	),			
    		
    );
    
    $args['tax_query'] = array(
    	'relation' 		=> 'OR', #AND
    
    	array(
    		'taxonomy'	=> 'event_type',
    		'field'		=> 'term_id',
    		'terms'		=> $event_type_id
    	),			
    		
    );
    
    $query = new WP_Query( $args );
    if( $query->have_posts() ) : 
    
    	while( $query->have_posts() ): $query->the_post(); ?>
        	<span><?php $date = get_field('event_start_date');
    		$starttime = get_field('event_start_time');
    		echo $date . " - " . $starttime; ?></span></br> 
    	<?php endwhile;  wp_reset_postdata();
    
    endif;

    Code is totally untested.
    Also worth adding debugging code in:

    global $wpdb;
    
    // Print last SQL query string
    echo $wpdb->last_query;
    
    // Print last SQL query result
    echo $wpdb->last_result;
    
    // Print last SQL query Error
    echo $wpdb->last_error;

    You can see what the output of your query contains, it may then point you in the right direction

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.