I wrote a short code to work with an event plugin (the plugin creates the custom post type of event). I want to pull all future events from the database that use a specific category and list them on the page in order of the event date. The events are showing up but the category selection is not working and it is also showing past events.
Here is my shortcode:
/* Exhibitions Shortcode */
add_shortcode( 'event_list', 'eventList' );
function eventList( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'event',
'tax' => '',
'post_status' => 'publish',
'date_query' => array(
array(
'column' => 'event_start_date',
'after' => date('F j, Y'),
),
),
'posts_per_page' => '',
'order' => 'ASC',
'orderby' => 'event_start_date',
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="eventListing">
<div class="column oneThird">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
</div>
<div class="column twoThird last">
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<?php echo get_the_content( $more_link_text, $stripteaser ); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
/* END Exhibitons */
the shortcode in use is [event_list tax="exhibitions" posts_per_page="3"]
UPDATED CODE With Categories working:
/* Exhibitions Shortcode */
add_shortcode( 'event_list', 'eventList' );
function eventList( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'event',
'tax_query' => array(
array(
'taxonomy' => 'event-categories',
'field' => 'slug',
'terms' => $atts['tax'],
),
),
'post_status' => 'publish',
'date_query' => array(
'after' => date('F j, Y'),
'column' => 'event_start_date',
),
'posts_per_page' => $atts['posts_per_page'],
'order' => 'ASC',
'orderby' => 'event_start_date',
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="eventListing">
<div class="column oneThird">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
</div>
<div class="column twoThird last">
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<?php echo get_the_content( $more_link_text, $stripteaser ); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
/* END Exhibitons */