0

So I have a WP_Query, that displays posts from a custom post type. It only displays posts that have a date in the future, not ones that have past.

I now want to include tax_query in this WP_Query. So that it only displays custom posts that belong to a specific term, in a custom taxonomy called event categories. My code doesn't seem to work since i've added the tax_query part. It doesn't display any messages in the browser at all. How can I make it work with the tax_query part added please?

<?php 


  $args = array(
    'post_type' => 'event',
    'posts_per_page' => 100,
    'meta_query' => array( 'key' => '_start_ts', 'value' => current_time('timestamp'), 'compare' => '>=', 'type'=>'numeric' ),
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_key' => '_start_ts',
    'meta_value' => current_time('timestamp'),
    'meta_value_num' => current_time('timestamp'),
    'meta_compare' => '>=',
'tax_query' => array(
  // second array
    array(
      'taxonomy' => 'event-categories',
      'field'    => 'slug', 
      'term' => $termst
    )
     // end second array
  ),

  );

  // The Query
  $query = new WP_Query( $args );

  // The Loop
  while($query->have_posts()):
  $query->next_post();
  $id = $query->post->ID;
  echo '<li>';
  echo get_the_title($id);
  echo ' - '. get_post_meta($id, '_event_start_date', true);
  echo '</li>';
  endwhile;

  // Reset Post Data
  wp_reset_postdata();
  • 1
    Where is $termst defined and what is it? – TheDeadMedic May 14 '15 at 18:35
0

I can't comment yet, but I would suggest first to modify your loop

<?php
// The Query
$query = new WP_Query( $args );

// The Loop
<?php if ( $query->have_posts() ) : ?>
    <?php while( $query->have_posts() ) : $query->the_post(); ?>
        <li><?php the_title(); ?> - <?php get_post_meta( get_the_ID() , '_event_start_date', true ); ?></li>
    <?php endwhile; ?>
<?php else : ?>
    <pre><?php print_r( $query ); ?></pre>
    <pre><?php print_r( $args ); ?></pre>
<?php endif; ?>

That way we can see the return from WP_Query. It could be it didn't find any articles, could be an error, I won't know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.