簡體   English   中英

WordPress-按標簽和類別的get_posts

[英]Wordpress - get_posts by tags and category

以下未返回任何結果。 我想按類別和多個標簽過濾。 你能看出我做錯了嗎?

$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
) );

嘗試這樣的事情:

$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
) );

檢查它是否有效。

請參閱以下示例,以按標簽和類別獲取帖子

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM