0

I have a WP query that searches for any custom posts under type 'tribe_events' tagged as 'featured' under the taxonomy 'tribe_events_cat'.

So, just to be clear:
post_type = 'tribe_events'
taxonomy = 'tribe_events_cat'
terms = 'featured', 'streamteam'

However, it returns nothing when outside of page ID 199. I can comment-out the tax_query and it works but returns all posts, not just ones tagged as 'featured'.

  if(is_page(199)) {
    $tribe_events_cat = array( 'featured','streamteam' );
  } else {
    $tribe_events_cat = 'featured';
  }

  // The Query
  $event_args = array (
    'post_type' => 'tribe_events',
    'posts_per_page' => 1,
    'tax_query' => array (
      array (
        'taxonomy' => 'tribe_events_cat',
        'field' => 'slug',
        'terms'     => $tribe_events_cat,
      ),
    ),
    'meta_query' => array(
      array(
        'key' => '_EventStartDate',
        'value' => date("Y-m-d"),
        'compare' => '>=',
        'type' => 'DATE',
      ),
    ),
    'meta_key'          => '_EventStartDate',
    'order'             => 'ASC'
  );
  $event_query = new WP_Query( $event_args );

  // The Loop
  if ( $event_query->have_posts() ) {
    while ( $event_query->have_posts() ) {
    $event_query->the_post();
    ?>
    POST CONTENT HERE
    <?php
    }
  }

What's wrong with my tax_query?!

EDIT:

This answer solved my initial problem of no posts being returned from my query
https://stackoverflow.com/questions/30213495/wp-query-returns-no-results
Apparently there was a filter conflicting with my query.

However, I'm still having an issue.. I only want to return posts with both 'featured' AND 'streamteam' terms when on page ID 199. Right now it's returning posts that only belong to the 'featured' term.

2
  • 1
    In the intro, you wrote taxonomy = 'tribe_events-cat', in the query it is tribe_events_cat, is that just a typo?
    – janh
    Oct 12, 2017 at 21:04
  • Yes, sorry, typo is fixed.
    – jaker6492
    Oct 12, 2017 at 21:28

3 Answers 3

0

Ok, I've just tested this in my installation (with a taxonomy that exists here). Usually, I just quickly throw stuff into the functions.php and look at the SQL that's generated (print $event_query->request;). That didn't work and the tax_query always generated 0 = 1 thus making sure that there were no results.

Finally, heureka, the taxonomy wasn't registered at that stage. Running it later and it works as expected.

Might something similar happen for you? Is your taxonomy-registration-code maybe working differently based on what page is currently requested?

In any case, please also add the SQL that is generated to the question. That might shine some light. How is your taxonomy added, CPTUI or similar, or your own code?

Maybe throw in

foreach ( get_post_types() as $post_type ) {
   echo '<p>' . $post_type . ": " . var_export(get_object_taxonomies($post_type), true) . '</p>';
}

Just to be sure.

4
  • From running foreach ( get_post_types() as $post_type )... just above my $event_args = array declaration I get: 'tribe_events: array ( 0 => 'post_tag', 1 => 'tribe_events_cat', )', which means that the taxonomy tribe_events_cat is registered at the time of the wp_query call, right?
    – jaker6492
    Oct 12, 2017 at 22:33
  • Yeah, that looks fine. Have you looked at the SQL (and manually ran it against the database)?
    – janh
    Oct 12, 2017 at 22:59
  • Yes, ran against the database and it returned post id 312, which is a post that only belongs to 'featured' and NOT 'streamteam'. Is there a chance my tax_query is pulling in posts from 'streamteam' OR 'featured'? I could have sworn that passing an array into a single 'terms' parameter in the tax_query meant automatically using an AND operator?
    – jaker6492
    Oct 13, 2017 at 18:49
  • Yeah, an array will basically be translated to IN(val1, val2). You'll have to use two single-term-subqueries, if you want an AND.
    – janh
    Oct 13, 2017 at 19:00
0

You used this 'meta_key' => '_EventStartDate', for which purpose? Everything looks fine, did you try commenting the meta_query

1
  • Yeah, I tried commenting out the meta_query, no luck. Still pulling in posts that belong to streamteam OR featured. I want it to only pull posts that belong to both streamteam AND featured when on page ID 199. 'meta_key' => '_EventStartDate' is ordering the posts by '_EventStartDate', which are being pulled in by my meta_query. The meta_query is pulling in posts that have an '_EventStartDate' that occurs in the future.
    – jaker6492
    Oct 13, 2017 at 19:03
0

Thank you for all your help! Here's what I ended up going with:

if(is_page(199)) {
  // The Query
  $event_args = array (
    'post_type' => 'tribe_events',
    'posts_per_page' => 1,
    'suppress_filters' => true,
    'tax_query' => array (
      'relation' => 'AND',
      array (
        'taxonomy' => 'tribe_events_cat',
        'field' => 'slug',
        'terms' => 'streamteam',
      ),
      array (
        'taxonomy' => 'tribe_events_cat',
        'field' => 'slug',
        'terms' => 'featured',
      ),
    ),
    'meta_query' => array(
      array(
        'key' => '_EventStartDate',
        'value' => date("Y-m-d"),
        'compare' => '>=',
        'type' => 'DATE',
      ),
    ),
    'meta_key' => '_EventStartDate',
    'order' => 'ASC'
  );
  } else {
    // The Query
    $event_args = array (
      'post_type' => 'tribe_events',
      'posts_per_page' => 1,
      'suppress_filters' => true,
      'tax_query' => array (
        array (
          'taxonomy' => 'tribe_events_cat',
          'field' => 'slug',
          'terms' => 'featured',
        ),
      ),
      'meta_query' => array(
        array(
          'key' => '_EventStartDate',
          'value' => date("Y-m-d"),
          'compare' => '>=',
          'type' => 'DATE',
        ),
      ),
      'meta_key' => '_EventStartDate',
      'order' => 'ASC'
    );
  }
$event_query = new WP_Query( $event_args );

// The Loop
if ( $event_query->have_posts() ) {
  while ( $event_query->have_posts() ) {
  $event_query->the_post();
  ?>
  POST CONTENT HERE
  <?php
  }
}

Instead of using an array ($tribe_events_cat) for my terms subquery, I had to use two single-term terms subqueries, turning my tax_query into an AND request for posts in featured AND streamteam.

Also, I had to add 'suppress_filters' => true, to each args array to fix some weird filtering issues happening outside of my query that were affecting my results.

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.

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