2017-04-13 59 views
0

中結合tax_query和meta_query我嘗試使用預定義的meta值獲取自定義帖子。查詢不返回任何內容。在query_posts()

$args = array 
    (
     'post_type' => 'item', 
     'nopaging' => true, 
     'tax_query' => array 
     ( array 
      (
      'taxonomy' => $taxonomy, 
      'field' => 'term_taxonomy_id', 
      'terms' => $term_id 
      ) 
     ), 
     'meta_query' => array(
      array(   
       'key'  => 'from_import', 
       'value' => '1', 
      ) 
     ) 
    ); 
    $posts = query_posts($args); 

當我刪除meta_query或tax_query它工作正常。我如何將它結合起來?

回答

0

這應該修復你的代碼,我相信。我沒有真正運行它,但嘗試一下。你們每個人都有一個額外的'數組()'。

$args = array 
    (
     'post_type' => 'item', 
     'nopaging' => true, 
     'tax_query' => array 
     (
      'taxonomy' => $taxonomy, 
      'field' => 'term_taxonomy_id', 
      'terms' => $term_id 
     ), 
     'meta_query' => array(
      'key'  => 'from_import', 
      'value' => '1' 
     ) 
    ); 
    $posts = query_posts($args); 
+0

謝謝,但沒有第二個箭頭'tax_query'查詢無法過濾結果。 – EOnegin

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