0

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;?>

1 Answer 1

1

Your tax query should be an array of arrays.

Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays). This construct allows you to query multiple taxonomies by using the relation parameter in the first (outer) array to describe the boolean relationship between the taxonomy arrays.

https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

'tax_query' => array(
  // second array
    array(
      'taxonomy' => 'event-categories',
      'field'    => 'slug', 
      'terms' => $termst
    )
  // end second array
  ),

Second, the "key" is terms not term as you have it.

2
  • Hi, I have tried the code you provided, but now I get 'Oops, there are no posts.' when there are posts. I have added the code under updated,I would really appreciate it if you could have a look for me please.
    – user28566
    May 14, 2015 at 16:08
  • @mattnewbie : See the edit
    – s_ha_dum
    May 15, 2015 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.