好吧,我想我有东西要给你!正如你所知,我也遇到了同样的问题,当我循环遍历每个分类法时,我发现我生成了25-35个查询,这太疯狂了!所以我决定尝试一下term relationships表上的一些MySQL,看看是否有更快的方法来查询数据。
在我认识的一位优秀开发人员(Mac McDonald)的帮助下http://wordpress.mcdspot.com/ ), 我创建了一个函数,它在“其他”分类法中发出一个请求并返回一个术语ID数组。您可以先准备要查询的术语ID,如下所示;
//我们要检查术语的分类法$contraction\\u taxonomy\\u terms=get\\u terms($taxonomy);
//new array, with current term id, very important!
$sibling_term_ids = array( $this_term->term_id );
foreach( $opposite_taxonomy_terms as $term )
{
//collect all ids, ready to build the MySQL query
$sibling_term_ids[] = $term->term_id;
}
$imploded = implode(\', \', $sibling_term_ids );
$results = rd_get_opposite_terms( $imploded, $this_term->term_id );
我正在获取要“检查”的术语,然后将它们添加到一个数组中,该数组必须包含您要查询的主要术语ID。然后,我将其内爆,以获得MySQL查询的逗号分隔列表。在
rd_get_opposite_terms()
作用
function rd_get_opposite_terms( $imploded = array(), $current_term_id = FALSE )
{
if( empty( $imploded ) || ! $current_term_id )
return FALSE;
global $wpdb;
//perform a query to find terms that have posts in both the $current_term_id (from one taxonomy) and terms in $imploded (term IDs from another taxonomy). Returns IDs as an array.
return $wpdb->get_col( \'SELECT tr1.term_taxonomy_id FROM wp_term_relationships tr1 WHERE tr1.term_taxonomy_id IN ( \' . $imploded . \' ) AND tr1.object_id IN (SELECT DISTINCT object_id FROM wp_term_relationships tr2 WHERE tr2.term_taxonomy_id = \' . $current_term_id . \' ) GROUP BY tr1.term_taxonomy_id\' );
}
我承认,目前这已接近我开发能力的顶峰,但我认为这是一个比我原来拥有的更好的解决方案,我希望它也能帮助你!