php - Wordpress - 同时使用 meta_query 和 tax_query?

标签 php wordpress

我一直在尝试在 WP_Query 参数中同时使用 tax_querymeta_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
        )
    )
)

最佳答案

这是我在我的场景中使用的工作片段,根据需要进行调整以满足您的需求。

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

关于php - Wordpress - 同时使用 meta_query 和 tax_query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23134141/

相关文章:

php - 使用 PHP 清除表值

javascript - 实现打开/关闭标签功能的最佳方式

php - 在 SQL 语句中使用 PHP 变量

php - 如何在wordpress中更改像seo友好的url

php - 当所有对象都属于同一类型时,我可以省略 PHP in_array() 中的 strict 参数吗?

php - Laravel 命令-> 选择循环

javascript - jQuery `submit` 方法忽略使用 GET 发送的 POST。为什么?

php - 如何将 query.ajax 的 json 响应分配给变量?

php - 自定义 wordpress 主题 : layout images not displaying

php - Wordpress - 按日期范围获取帖子