如何合并以下查询?
问题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;
}
第二个查询在隔离状态下运行良好。
我不明白的是如何将这两者结合起来。
正确的方法是执行这两个查询,然后以某种方式在最后合并吗?(我尝试了几种数组合并技术,但结果查询似乎变得不稳定)。
或者,有没有一种方法可以实现单个更高效的查询?