搜尋

首頁  >  問答  >  主體

為meta_query ORtax_query設定關係“OR”

我正在嘗試使用 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粉101708623P粉101708623292 天前1545

全部回覆(1)我來回復

  • P粉099000044

    P粉0990000442024-04-05 00:33:11

    以下是如何在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' => '='
        ),
      ),
    );

    回覆
    0
  • 取消回覆