你好@shaun:
@Rarst的正确之处在于adding this functionality to WordPress v3.1, 但它不会在这里一段时间,我相信你现在需要它。你可以试着grab the code they are working on 试着让它发挥作用,你可以the plugin Rarst和躯体都提到了,或者你可以在下面的主题中加入你需要的代码。
以下代码是posts_where
挂钩以添加tax_terms
具有以下格式值的查询变量(即冒号逗号),其中值为termslugs:
"{$varname1}:{$value1},{$varname2}:{$value2},...{$varnameN}:{$valueN}"
这是一个独立的
.php
您可以在拉出
add_action
电话和
tax_terms_where()
在主题中发挥作用
functions.php
文件(或输入到
.php
您自己的一个插件的文件。)
<?php
/*
* Adds "tax_terms" to WP_Query()
*
* See: http://lists.automattic.com/pipermail/wp-hackers/2010-October/035258.html
* See: http://wordpress.stackexchange.com/questions/2255/
*
*/
header(\'Content-Type:text/plain\');
include "wp-load.php";
add_action(\'posts_where\',\'tax_terms_where\',10,2);
$result = new WP_Query(\'post_type=portfolio&tax_terms=project_type:A,package:A\');
foreach($result->posts as $post) {
echo "{$post->post_title}\\n";
}
function tax_terms_where($where,$wp_query) {
if (isset($wp_query->query)) {
$query = $wp_query->query;
if (is_string($query))
parse_str($query,$query);
if (is_array($query) && isset($query[\'tax_terms\'])) {
global $wpdb;
$tax_terms = explode(\',\',$query[\'tax_terms\']);
foreach($tax_terms as $tax_term) {
list($taxonomy,$term) = explode(\':\',$tax_term);
$sql = <<<SQL
AND $wpdb->posts.ID IN (
SELECT tr.object_id
FROM $wpdb->term_relationships AS tr
INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id
WHERE tt.taxonomy=\'%s\' AND t.slug=\'%s\'
)
SQL;
$where .= $wpdb->prepare($sql,$taxonomy,$term);
}
}
}
return $where;
}
例如,如果这不是您所需要的,因为您希望
$term->term_id
而不是匹配
$term->slug
那么,如果您熟悉SQL和
.php
.