我需要有多个分类法和动态术语的wp\\U查询。多个术语对应一个分类法。就像一种分类法Color 它有条款blue,green,red 第二种分类法是Size 和条款7,8,9 这种分类法是Price 其条款是500$,600$ 等等
我需要根据复选框筛选结果,就像在任何电子商务网站上一样。先说我选择blue 颜色则产品属于blue 如果我选择green 那么产品就是属于两者的blue 和green 但现在如果我选择7 那么产品将属于blue,green having size 7 .
希望你能得到我所需要的。我在codex上看到了多个分类法,但它只有2个静态分类法,但在我的例子中,它是动态的,可以超过5个。
在法典中,它是代码。。
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'movie_genre\',
\'field\' => \'slug\',
\'terms\' => array( \'action\', \'comedy\' )
),
array(
\'taxonomy\' => \'actor\',
\'field\' => \'id\',
\'terms\' => array( 103, 115, 206 ),
\'operator\' => \'NOT IN\'
)
)
);
$query = new WP_Query( $args );$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'movie_genre\',
\'field\' => \'slug\',
\'terms\' => array( \'action\', \'comedy\' )
),
array(
\'taxonomy\' => \'actor\',
\'field\' => \'id\',
\'terms\' => array( 103, 115, 206 ),
\'operator\' => \'NOT IN\'
)
)
);
$query = new WP_Query( $args );
但是,如果我有超过2个,例如5个(动态)。
请给我任何解决方案。
我还编写了Wp query的query intead,但不起作用,它添加了大小为7的蓝色、绿色结果,而不是过滤结果。
这里是查询。
$taxonomy_name = "\'" . implode("\',\'", $taxonomy_name_ar) . "\'";
$term_name = "\'" . implode("\',\'", $term_name_ar) . "\'";
$querystr = "
SELECT *
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = \'product\'
AND $wpdb->posts.post_status = \'publish\'
AND $wpdb->term_taxonomy.taxonomy in (".$taxonomy_name.")
AND $wpdb->terms.slug in (".$term_name.")
ORDER BY $wpdb->posts.post_date DESC
LIMIT 10
";
echo $querystr;
$pageposts = $wpdb->get_results($querystr, OBJECT);
Note $tanomy\\u name有许多分类法,如颜色、大小、价格,还有一些术语,如7、8、蓝色、绿色
UPDATE : AFTER ANSWER
$tax_query = array( \'relation\' => \'AND\' );
foreach($taxonomy_array as $taxonomy_array_value){
$term_value_array = array();
foreach($term_array[$taxonomy_array_value] as $term_value){
array_push($term_value_array,$term_value);
}
$term_name = "\'" . implode("\',\'", $term_value_array) . "\'";
$tax_query[] = array(
\'taxonomy\' => $taxonomy_array_value,
\'field\' => \'slug\',
\'terms\' => array($term_name)
);
}
$args = array(
\'post_type\' => \'product\',
\'tax_query\' => $tax_query
);
最合适的回答,由SO网友:birgire 整理而成
以下是动态多分类查询的一个想法:
// construct the tax-query:
$tax_query = array( \'relation\' => \'AND\' );
// check if movie_genre taxonomy is to be included
if( $bMovieGenre ){
$tax_query[] = array(
\'taxonomy\' => \'movie_genre\',
\'field\' => \'slug\',
\'terms\' => array( \'action\', \'comedy\' )
);
}
// check if actor taxonomy is to be included
if( $bActor ){
$tax_query[] = array(
\'taxonomy\' => \'actor\',
\'field\' => \'id\',
\'terms\' => array( 103, 115, 206 ),
\'operator\' => \'NOT IN\'
);
}
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => $tax_query,
);
$query = new WP_Query( $args );
在哪里
$bActor
和
$bMovieGenre
根据用户输入构造。
通常,如果用户输入数组如下所示:
$selected = array(
array( \'taxonomy\' => \'movie_genre\' ,
\'terms\' => array( \'action\' , \'comedy\') ,
\'field\' => \'slug\' ,
\'operator\' => \'IN\' ,
),
array( \'taxonomy\' => \'actor\' ,
\'terms\' => array( 103, 115, 206 ) ,
\'field\' => \'slug\' ,
\'operator\' => \'NOT IN\' ,
),
);
然后,您可以尝试构建动态分类查询:
$tax_query = array( \'relation\' => \'AND\' );
foreach( $selected as $sel ){
$tax_query[] = array(
\'taxonomy\' => $sel[\'taxonomy\'] ,
\'terms\' => $sel[\'terms\'] ,
\'field\' => $sel[\'field\'] ,
\'operator\' => $sel[\'operator\'] ,
);
}
或者只是
$tax_query = array( \'relation\' => \'AND\' );
foreach( $selected as $sel ){
$tax_query[] = $sel;
}