I have a custom post type called event. Custom taxonomy called event categories, which has various terms. I want to show all the events that belong to a certain custom taxonomy term on a archive page.
I am including the custom taxonomy term wrongly, as it displays all the events and not just ones that are classified in a term of the custom taxonomy. It displays only events that are in the future, which I want.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $termst = $term->slug;
$current_date = date('Y-m-d'); //Get the current date in the same format as date fields normally store in db
$args = array(
'post_type' => 'event',
'posts_per_page' => 4, // Number of event posts you want to query
'meta_key' => '_event_start_date',
'orderby' => '_event_start_date',
'order' => 'ASC', // Order posts starting with the closest date to current date
'tax_query' => array(
'taxonomy' => 'event-categories',
'field' => 'slug',
'term' => $termst
),
'meta_query' => array(
array(
'key' => '_event_start_date',
'value' => $current_date,
'compare' => '>=', //Compare $current_date with _event_start_date and show only the post with a date after or equal $current_date
'type' => 'DATE',
),
)
);
$query = new WP_Query( $args );
if($query ->have_posts()) :
while($query ->have_posts()) :
$query ->the_post();
?>
<h1><?php the_title() ?></h1>
<?php echo ' - '. get_post_meta($id, '_event_start_date', true) ?>
<?php
endwhile;
else:
?> Oops, there are no posts.
<?php endif;?>
How can I include the current taxonomy term here, so that it only shows events that have been assigned to a specific term? Thank-you!
UPDATED: I have changed my code to the recommendation, but now it says Oops, there are no posts.
. Can anyone take a look and tell me what is wrong please? There are posts.
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $termst = $term->slug;
$current_date = date('Y-m-d'); //Get the current date in the same format as date fields normally store in db
$args = array(
'post_type' => 'event',
'posts_per_page' => 4, // Number of event posts you want to query
'meta_key' => '_event_start_date',
'orderby' => '_event_start_date',
'order' => 'ASC', // Order posts starting with the closest date to current date
'tax_query' => array(
// second array
array(
'taxonomy' => 'event-categories',
'field' => 'slug',
'term' => $termst
)
// end second array
),
'meta_query' => array(
array(
'key' => '_event_start_date',
'value' => $current_date,
'compare' => '>=', //Compare $current_date with _event_start_date and show only the post with a date after or equal $current_date
'type' => 'DATE',
),
)
);
$query = new WP_Query( $args );
if($query ->have_posts()) :
while($query ->have_posts()) :
$query ->the_post();
?>
<h1><?php the_title() ?></h1>
<?php echo ' - '. get_post_meta($id, '_event_start_date', true) ?>
<?php
endwhile;
else:
?> Oops, there are no posts.
<?php endif;?>