当我在类别ID中传递数组时,它可以工作,但是当我传递php变量时,它将无效

时间:2016-03-08 09:24:56

标签: wordpress taxonomy

我想获取自定义帖子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);

1 个答案:

答案 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);