Wordpress - 按标签和类别的get_posts

时间:2015-08-25 12:18:25

标签: php wordpress

以下内容未返回任何结果。我想按类别和多个标签进行过滤。你能看出我做错了吗?

$tags = array( "blah-blah", "sausage" );

$posts = get_posts( array(
    'posts_per_page'   => 3,
    'offset'           => 0,
    'category'         => $categoryID,
    'orderby'          => 'date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => true,
    'tag'              => implode( ",", $tags )
) );

修改

这似乎有效!

$posts = get_posts( array(
    'posts_per_page'   => 3,
    'offset'           => 0,
    'category'         => $categoryID,
    'orderby'          => 'date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => true,
    'tag_slug__in'     => $tags
) );

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$posts = get_posts( array(
    'posts_per_page'   => 3,
    'offset'           => 0,
    'category__and'         => $categoryID,
    'orderby'          => 'date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => true,
    'tag__in'     => $tags
) );

检查它是否有效。

答案 1 :(得分:0)

请参阅以下示例,按标签获取帖子&类

global $wp_query;
        $args = array(
        'category__and' => 'category', 
        'tag__in' => 'post_tag', //must use tag id for this field
        'posts_per_page' => -1); //get all posts

$posts = get_posts($args);
        foreach ($posts as $post) :
  //do stuff 
     endforeach;