2017-04-13 24 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' 
       ) 
      ) 
     ) 
    );