WordPress – using meta_query and tax_query together?

I’ve been trying to use tax_query and meta_query together in a WP_Query argument, but that doesn’t seem to be working for some reason.

My code is:

Read More
$args = array (
    'meta_key' => 'ratings_average',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'      => 'eventstart',
            'compare'  => '>=',
            'value'    => $tonight, 
        ),
        array(
            'key'      => 'eventstart',
            'compare'  => '<',
            'value'    => $tomorow, 
        ),
    ),
    'tax_query' => array(
        array(
            'taxonomy' => 'Music',
            'field'    => 'slug',
            'terms'    => 'fri'
        ),
    ),
);  

$my_query = new WP_Query( $args );

Does anyone know where I’m going wrong in the code. Any help would be appreciated.

After print_r($args), it gives me the following result:

Array (
    [meta_key] => ratings_average
    [orderby] => meta_value_num
    [order] => DESC
    [meta_query] => Array (
        [relation] => AND
        [0] => Array (
            [key] => eventstart
            [compare] => >=
            [value] => 17/04/14 00:00
        )
        [1] => Array (
            [key] => eventstart
            [compare] => <
            [value] => 18/04/14 00:00
        )
    )
    [tax_query] => Array (
        [0] => Array (
            [taxonomy] => Music
            [field] => slug
            [terms] => fri
        )
    )
)

Related posts

Leave a Reply

2 comments

  1. Here’s a working snippet I’m using for my scenario, tweak as needed to match your needs.

    // Bring post from the global context (if not present already).
    global $post;
    
    // Define the post_type's to query for.
    $post_types = array( 'event', 'post', 'book' );
    
    // Do the weird query. 
    // Play with, or add arguments as needed https://codex.wordpress.org/Class_Reference/WP_Query
    $results = WP_Query(
            array(
                'post_type' => $post_types,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'category',
                        'terms' => wp_get_post_categories( $post->ID )
                    )
                ),
                'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key'     => 'presenters_people',
                        'value'   => $post->ID,
                        'compare' => 'LIKE'
                    ),
                    array(
                        'key'     => 'author',
                        'value'   => $post->ID,
                        'compare' => 'LIKE'
                    )
                )
            )
        );
    
  2. Try this code,

    $args = array (
        'meta_key' =>'ratings_average',
        'orderby'=>'meta_value_num',
        'order' =>'DESC',
        'meta_query' => array(
            'relation' => 'AND',
            array(
            'key'       => 'eventstart',
            'compare'   => '>=',
            'value'     =>  $tonight,   
            ),
            array(
            'key'       => 'eventstart',
            'compare'   => '<',
            'value'     =>  $tomorow,   
            )
        ),
        'tax_query' => array(
            array(
            'taxonomy' => 'Music',
            'field' => 'slug',
            'terms' => 'fri',
            'operator'  => 'IN'
            )
        ),
    );  
    
    $my_query = new WP_Query($args);