2017-03-27 65 views
1

我想在meta_query中做到這一點,但迄今沒有成功。Wp_Query meta_query無法正常工作

WHERE tm_url_1="" AND tm_url_2="" AND (tm_url_3="" OR tm_embed_code=!="") AND (tm_url_3!="" OR tm_embed_code=="") 

這裏是WP_Query。一旦我添加OR子句沒有返回。

$args = array(
       'post_status'   => $post_status, 
       'posts_per_page'  => $posts_count, 
       'ignore_sticky_posts' => 1, 
       'post_type'   => $post_type, 
       'order_by'   => $order_by, 
       'order'    => $order, 
       "meta_query"  => array(
        array('relation' => 'AND', 
         array(
          'key'  => 'tm_url_1', 
          'value' => '', 
          'compare' => '=' 
         ), 
         array(
          'key'  => 'tm_url_2', 
          'value' => '', 
          'compare' => '=' 
         ), 
         array(
          'relation' => 'OR', 
          array(
           'key'  => 'tm_url_3', 
           'value' => '', 
           'compare' => '=' 
          ), 
          array(
           'key'  => 'tm_embed_code', 
           'value' => '', 
           'compare' => '!=' 
          ) 
         ), 
         array(
          'relation' => 'OR', 
          array(
           'key'  => 'tm_url_3', 
           'value' => '', 
           'compare' => '!=' 
          ), 
          array(
           'key'  => 'tm_embed_code', 
           'value' => '', 
           'compare' => '=' 
          ) 
         ) 
        ) 
        ) 
      ); 

什麼都沒有返回。 meta_query在哪裏出錯?

回答

1

您應該刪除在media_query參數的第一個「陣列」:

"meta_query"  => 
       array(
        'relation' => 'AND', 
        array(
         'key'  => 'tm_url_1', 
         'value' => '', 
         'compare' => '=' 
        ), 
        array(
         'key'  => 'tm_url_2', 
         'value' => '', 
         'compare' => '=' 
        ), 
        array(
         'relation' => 'OR', 
         array(
          'key'  => 'tm_url_3', 
          'value' => '', 
          'compare' => '=' 
         ), 
         array(
          'key'  => 'tm_embed_code', 
          'value' => '', 
          'compare' => '!=' 
         ) 
        ), 
        array(
         'relation' => 'OR', 
         array(
          'key'  => 'tm_url_3', 
          'value' => '', 
          'compare' => '!=' 
         ), 
         array(
          'key'  => 'tm_embed_code', 
          'value' => '', 
          'compare' => '=' 
         ) 
        ) 
       ) 

見:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

1

這裏是添加meta_query你想要的條件的正確方法。

$args = array(
    'post_status' => $post_status, 
    'posts_per_page' => $posts_count, 
    'ignore_sticky_posts' => 1, 
    'post_type' => $post_type, 
    'order_by' => $order_by, 
    'order' => $order, 
    'meta_query' => array(
     array(
      'key' => 'tm_url_1', 
      'value' => '', 
     ) , 
     array(
      'key' => 'tm_url_2', 
      'value' => '', 
     ) , 
     array(
      'relation' => 'OR', 
      array(
       'key' => 'tm_url_3', 
       'value' => '', 
      ) , 
      array(
       'key' => 'tm_embed_code', 
       'value' => '', 
       'compare' => '!=' 
      ) 
     ) , 
     array(
      'relation' => 'OR', 
      array(
       'key' => 'tm_url_3', 
       'value' => '', 
       'compare' => '!=' 
      ) , 
      array(
       'key' => 'tm_embed_code', 
       'value' => '', 
      ) 
     ) 
    ) 
); 

注意,我在某些陣列刪除默認值compare假設它是=爲您的病情。對此的參考可以看到here