为meta_query ORtax_query设置关系'OR”
P粉101708623
P粉101708623 2024-04-04 20:01:43
0
1
1337

我正在尝试使用 WP_Query 创建包含 meta_query 元素和tax_query 元素的查询。更关键的是,我不想找到满足两个条件(和 AND 子句)的结果,我想找到满足其中一个或另一个的条件(OR 子句)。

$loop = new WP_Query( array( 
            'post_type' => 'tickets',
            'paged' => $paged,
            'order'     => $order,
            'orderby'   => $orderby,
                'meta_query' => array(
                    array(
                    'key' => 'tps_email', // Use 'key' for author
                    'value' => 'web@data.com', // Replace with the author ID you want to query
                    'compare' => 'LIKE', // Use '=' to match the author ID
                    ),  
                ),  
                'tax_query' => array(
                    array(
                    'taxonomy' => 'topic', // Replace with your custom taxonomy name
                    'field' => 'slug', // You can use 'id', 'slug', or 'name' depending on how you want to identify the term
                    'terms' => 'housekeeping', // Replace with the slug of the specific term you want to query
                    ),
                ),
            )
            );

我接受这样的输出

P粉101708623
P粉101708623

全部回复(1)
P粉099000044

以下是如何在meta_query和tax_query之间设置“OR”关系:

$args = array(
   'post_type' => 'tickets',
   'paged' => $paged,
   'order'     => $order,
  'tax_query' => array(
    'relation' => 'OR', // Set the relationship to OR
    array(
        'taxonomy' => 'topic',
        'field' => 'slug',
        'terms' => 'housekeeping',
    ),
    array(
        'taxonomy' => 'your_taxonomy_2',
        'field' => 'slug',
        'terms' => 'term_slug_2',
    ),
  ),
  'meta_query' => array(
    'relation' => 'OR', // Set the relationship to OR
    array(
        'key' => 'tps_email',
        'value' => 'web@data.com',
        'compare' => '='
    ),
    array(
        'key' => 'custom_field_2',
        'value' => 'custom_value_2',
        'compare' => '='
    ),
  ),
);
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!