我有一个自定义帖子类型(“产品”)的多个分类法,我希望人们从列表中选择是否要包括一个或多个术语,以及是否要排除一个或多个术语。
到目前为止,我提出的表格是:
<form method="post" name="en_tax" >
<?php $terms = get_terms( array(
\'taxonomy\' => \'post_tag\',
\'hide_empty\' => false,
) );
foreach ($terms as $terms){
echo \'<input type="radio" name="\'.$terms->name.\'"\';
if (isset($terms->name) && $terms->name=="yes") echo "checked";
echo \'value="yes">With \';
echo \'<input type="radio" name="\'.$terms->name.\'"\';
if (isset($terms->name) && $terms->name=="no") echo "checked";
echo \'value="no">Without \'.$terms->name.\'\';
echo \'<br>\';
}
$cat = get_terms( array(
\'taxonomy\' => \'category\',
\'hide_empty\' => false,
) );
foreach ($cat as $cat){
echo \'<input type="radio" name="\'.$cat->name.\'"\';
if (isset($cat->name) && $cat->name=="yes") echo "checked";
echo \'value="yes">With \';
echo \'<input type="radio" name="\'.$cat->name.\'"\';
if (isset($cat->name) && $cat->name=="no") echo "checked";
echo \'value="no">Without \'.$cat->name.\'\';
echo \'<br>\';
}
?>
<input type="submit">
</form>
但我不知道如何将结果放入这样的查询中,每个no值都是“not IN”,每个yes值都是“IN”:
<?php $attachments = get_posts( array(
\'post_type\' => \'product\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'slug\',
\'terms\' => \'custom-tag\',
\'operator\' => \'NOT IN\',
),
array(
\'taxonomy\' => \'collection\',
\'field\' => \'slug\',
\'terms\' => \'custom-cat\',
)
),
) );
if ( $attachments ) {
foreach ( $attachments as $post ) {
setup_postdata( $post );
the_title();
echo \'<br>\';
}
wp_reset_postdata();
}?>
SO网友:Nicolai Grossherr
如果我没听错的话array
条件之一slug
或term_id
, 例如,取决于field
准确地说。
$args = array(
\'post_type\' => \'a_post_type\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'tax_one\',
\'field\' => \'slug\',
\'terms\' => array( \'action\', \'comedy\' ),
),
array(
\'taxonomy\' => \'tax_two\',
\'field\' => \'term_id\',
\'terms\' => array( 103, 115, 206 ),
\'operator\' => \'NOT IN\',
),
),
);
请参见
Codex: WP_Query对于表单,简而言之,将名称基本上设置为数组,例如:
<form method="post" name="en_tax" >
<input type="checkbox" name="post_variable[]" value="value 1">
<input type="checkbox" name="post_variable[]" value="value 2">
<input type="submit" />
</form>
然后可以获取变量
$_POST[ \'post_variable\' ]
, 这是一个值数组。