好的,所以這裏是我的破解。這有點哈克,但它的作品。最大的缺點是需要重新添加任何其他查詢變量,因爲在調用多個術語時,失敗會消除所有查詢變量。
此外,我沒有對多個分類法中的查詢進行測試。這隻適用於特定的分類。使用風險自負。
function multi_tax_terms($where) {
global $wp_query;
if (strpos($wp_query->query_vars['term'], ',') !== false && strpos($where, "AND 0") !== false) {
// it's failing because taxonomies can't handle multiple terms
//first, get the terms
$term_arr = explode(",", $wp_query->query_vars['term']);
foreach($term_arr as $term_item) {
$terms[] = get_terms($wp_query->query_vars['taxonomy'], array('slug' => $term_item));
}
//next, get the id of posts with that term in that tax
foreach ($terms as $term) {
$term_ids[] = $term[0]->term_id;
}
$post_ids = get_objects_in_term($term_ids, $wp_query->query_vars['taxonomy']);
if (!is_wp_error($post_ids) && count($post_ids)) {
// build the new query
$new_where = " AND wp_posts.ID IN (" . implode(', ', $post_ids) . ") ";
// re-add any other query vars via concatenation on the $new_where string below here
// now, sub out the bad where with the good
$where = str_replace("AND 0", $new_where, $where);
} else {
// give up
}
}
return $where;
}
add_filter("posts_where", "multi_tax_terms");
像上面這樣的手動查詢正是我最終使用的。現在,如果您不想一個接一個地運行多個get_posts()查詢(並且不這樣做),那麼這是唯一的方法。 – bobsoap 2010-08-16 03:00:09
這個查詢看起來可能適用於我想要做的事情,但是如果我想在一篇文章中搜索多個詞彙,該怎麼辦?我已經嘗試將OR更改爲AND(查詢的第9行),它似乎不起作用。 – jasonaburton 2012-08-13 12:27:50