Wordpress - 一起使用meta_query和tax_query?

时间:2014-04-17 13:13:07

标签: php wordpress

我一直试图在tax_query论证中一起使用meta_queryWP_Query,但由于某种原因,这似乎并不合适。

我的代码是:

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

有谁知道我在代码中出错的地方。任何帮助将不胜感激。

print_r($args)之后,它给出了以下结果:

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

2 个答案:

答案 0 :(得分:0)

试试这段代码,

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

答案 1 :(得分:0)

这是我用于我的方案的工作片段,根据需要调整以满足您的需求。

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