如何合并这两个POST查询?

时间:2019-06-18 作者:Robert Andrews

如何合并以下查询?

问题1:任何带有“主题”分类术语的帖子,例如“音乐”

问题2:任何附加到“公司”分类术语的帖子,例如“Spotify”,其本身具有上述“主题”术语,例如“音乐”(通过元字段“org\\u topics”)。

结果应该是所有与“音乐”相关的帖子,无论是直接标记该主题的帖子,还是其附属公司有该主题的帖子。

Query 1, regular:

  $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
  $query_args = array(
  // pagination
    \'nopaging\' => false,
    \'posts_per_page\' => 81,
    \'paged\' => $paged,
    // order
    \'order_by\' => \'date\',
    \'order\'   => \'DESC\',
    // posts
    \'post_type\' => \'any\',
    // taxonomies
    \'tax_query\' => array(
      \'relation\' => \'AND\',
      array(
        \'taxonomy\' => \'topic\',
        \'field\'    => \'slug\',
        \'terms\'    => \'music,
        \'include_children\' => false,
      ),
   ),
  );
  global $query_posts;
  $query_posts = new WP_Query($query_args);
  wp_reset_postdata();

Query 2, more complex:

我已经有了一个自定义函数(get_posts_with_extras) 它独立执行此查询。

它调用先前的自定义函数(get_terms_with_extras) 它首先获取具有匹配元的术语列表。我们使用它来生成一个要预先设置的术语ID数组WP_Query.

$post_results = get_posts_with_tax_and_meta(\'any\', \'company\', array(\'org_topics\'=>$queried_object->term_id), 81);

function get_posts_with_tax_and_meta(          // Get these posts...
   $post_type,                                 // 1. Get posts of this post type                        eg. \'viewpoint\'
   $taxonomy,                                  // 2. But only those with this taxonomy                  eg. \'company\'
   $tax_meta,                                  // 3. And the taxonomy term has this meta                eg. array(\'tags\'=>\'Marketing\'),
   $num_posts=6,                               // 4. Return this many posts                             eg. 6
   $tax_and_term=null,                         // 5. Constrain by a second taxonomy and term            eg. array(\'format\'=>\'oped\'),
   $offset                                     // 6. Offset posts by this number of posts               eg. 4
   // $paged                                   // 7. Pagination page
) {

  /*  1. First, get the Taxonomy Terms\' IDs */
  // These are the relevant Taxonomy Term objects

  $terms_list = get_terms_with_extras(
     $taxonomy,                                // 1. Get terms in the specified taxonomy                eg. \'company\'
     array(key($tax_meta)=>reset($tax_meta)),  // 2. Which also have these meta fields                  eg. array(\'tags\'=>\'media\')
     $post_type,                               // 3. And also have posts of the specified type          eg. \'viewpoint\'
     \'\',                                       // 4. But those posts must have this taxonomy and term   eg. array(\'format\'=>\'oped\')
     \'name\',                                   // 5. Order by value, corresponds to get_terms order_by  eg. \'name\'
     \'asc\'                                     // 6. Order direction, corresponds to get_terms order    eg. \'asc\'
                                               // 7. Limit term result, corresponds to get_terms number eg. 16
 );


  // But we only want the Terms\' IDs (to pass to the query below)...
  $terms_list_ids = array();
  foreach ($terms_list  as $term_single) {
       array_push($terms_list_ids, $term_single->term_id);
  }

  if ( !empty($tax_and_term) ) {
     $tax_and_term_query[] = array(
     \'taxonomy\' => key($tax_and_term),
     \'field\' => \'slug\',
     \'terms\' => reset($tax_and_term),
     );
   } else {
     $tax_and_term_query[] = null;
   }


  /* 2. Get the Posts with those Terms */
  // Query posts
  global $paged;
  $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
  $query_args = array(
  // pagination
    \'nopaging\' => false,
    \'posts_per_page\' => $num_posts,
    \'paged\' => $paged,
    // order
    \'order_by\' => \'date\',
    \'order\'   => \'DESC\',
    // posts
    \'post_type\' => $post_type,
    \'offset\' => $offset,
    // taxonomies
    \'tax_query\' => array(
      \'relation\' => \'AND\',
      array(
        \'taxonomy\' => $taxonomy,
        \'field\'    => \'id\',
        \'terms\'    => $terms_list_ids,
        \'include_children\' => false,
      ),
      $tax_and_term_query,
   ),
  );
  global $the_post_results; // Make it available everywhere
  // Get the posts and return them to whatever called this...
  $the_post_results = new WP_Query($query_args);
  wp_reset_postdata();
  return $the_post_results;


}
第二个查询在隔离状态下运行良好。

我不明白的是如何将这两者结合起来。

正确的方法是执行这两个查询,然后以某种方式在最后合并吗?(我尝试了几种数组合并技术,但结果查询似乎变得不稳定)。

或者,有没有一种方法可以实现单个更高效的查询?

1 个回复
SO网友:Robert Andrews

答案是“从第一个查询中的tax\\u查询参数中获取数组,将其插入到第二个查询中,并将关系更改为OR”。

换句话说,要在参数中使用具有以下内容的单个查询。。。

(如前所述,$terms_list_ids 已生成;它包含公司术语的ID,这些术语的元与当前主题术语匹配)。

答案归功于a Reddit user.

  // taxonomies
  \'tax_query\' => array(
    \'relation\' => \'OR\',
    // Posts related to a Company with this Topic...
    array(
      \'taxonomy\' => \'company\',
      \'field\'    => \'id\',
      \'terms\'    => $terms_list_ids,
      \'include_children\' => true, // <<< IMPORTANT
    ),
    // Or...
    // Posts with this Topic directly...
    array(
      \'taxonomy\' => \'topic\',
      \'field\'    => \'slug\',
      \'terms\'    => $topic_term->slug,
      \'include_children\' => false,
    ),
 ),

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post