我想获取自定义帖子tpye的帖子,当我直接在查询中传递cat id时,它适用于我发布所有类别ID的帖子但是当我在类似$ catid = array(154,153,155)的变量中传递这些类别ID时; 然后通过这个$ cat id它将无法正常工作
global $wpdb;
global $posts;
$catId = $args['categoryid'];
$perPage = $args['post_per_page'];
print_r($catId);
$posts_array = get_posts(
array(
'posts_per_page' => $perPage,
'post_type' => 'blog',
'tax_query' => array(
array(
'taxonomy' => 'blog_cat',
'field' => 'term_id',
'terms' => array($catId)
)
)
)
);
echo '<pre>';
print_r($posts_array);
答案 0 :(得分:1)
在$args['categoryid']
中分解逗号分隔的字符串以获取数组,然后在下面传递给函数
global $wpdb;
global $posts;
$catId = explode(',',$args['categoryid']);
$perPage = $args['post_per_page'];
$posts_array = get_posts(
array(
'posts_per_page' => $perPage,
'post_type' => 'blog',
'tax_query' => array(
array(
'taxonomy' => 'blog_cat',
'field' => 'term_id',
'terms' => $catId,
'operator' => 'IN'
)
)
)
);
echo '<pre>';
print_r($posts_array);