简体   繁体   中英

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:

$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
        )
    )
)

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'
                )
            )
        )
    );

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM