Php Wordpress分类查询,术语数组中的一个字符串变量起作用,一个不起作用';T
我正在Wordpress中构建一个网格,该网格具有自定义的分类术语,将用作过滤器。我一直在使用Php Wordpress分类查询,术语数组中的一个字符串变量起作用,一个不起作用';T,php,wordpress,Php,Wordpress,我正在Wordpress中构建一个网格,该网格具有自定义的分类术语,将用作过滤器。我一直在使用'terms'=>array()中的变量。一种类型的变量起作用,另一种不起作用 以下是查询: $args = array( 'posts_per_page' => -1, 'post_type' => 'plant', 'orderby' => 'name', 'order' => 'ASC', 'tax_query' => arra
'terms'=>array()中的变量。一种类型的变量起作用,另一种不起作用
以下是查询:
$args = array(
'posts_per_page' => -1,
'post_type' => 'plant',
'orderby' => 'name',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'trail_location',
'field' => 'slug',
'terms' => array($trailLocationList),
),
array(
'taxonomy' => 'bloom_time',
'field' => 'slug',
'terms' => array($bloomTime),
)
)
);
$loop = new WP_Query( $args );
我可以将查询字符串值转换为var,它可以工作:
$bloomTime = $_GET['bloom_time'];
'terms' => array($bloomTime).
静态值起作用:'terms'=>数组('loc1','loc2','loc3')
不起作用的是从现有分类法段塞数组创建变量
$terms = get_terms("trail_location");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
$trailLocationList[] = $term->slug;
}
}
$trailLocationList = implode("', '",$trailLocationList);
$trailLocationList = "'".$trailLocationList."'";
var输出我需要的确切字符串,正如上面静态示例('loc1','loc2','loc3')
中使用的那样,但由于某种原因,查询无法工作。我已经测试过了,两个变量都是字符串 根据我的评论,带逗号的字符串不是数组。
删除内爆步骤并使用$trailLocationList数组作为术语:
$terms = get_terms("trail_location");
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
$trailLocationList[] = $term->slug;
}
}
/* why?? dont do this
$trailLocationList = implode("', '",$trailLocationList);
$trailLocationList = "'".$trailLocationList."'";
*/
$args = array(
'posts_per_page' => -1,
'post_type' => 'plant',
'orderby' => 'name',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'trail_location',
'field' => 'slug',
//'terms' => array($trailLocationList), wrong
'terms' => $trailLocationList //correct
),
array(
'taxonomy' => 'bloom_time',
'field' => 'slug',
'terms' => array($bloomTime),
)
)
);
$loop = new WP_Query( $args );
带逗号的字符串不是数组如果删除了最后两行,它应该是fineWorks!我误解了你在先前评论中的意思。我仍然有点困惑,为什么我可以把静态逗号分隔的列表放在那里,然后它就可以工作了?我想你会在编写php代码和显示信息之间感到困惑。您的代码与此等价:terms=>array('one','two','three')请注意额外的双引号。php是弱类型的,但它不只是解释随机字符串