最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - get_posts() not working with multiple statuses

programmeradmin4浏览0评论

I am running the following code:

$posts = get_posts([
    'post_type'   => ['my-type', 'another-type'],
    'numberposts' => -1,
    'order'       => 'ASC',
    'orderby'     => 'title',
    'post_status' => ['published','future']
]);

It is only returning the status that appears last in the array. So in the above example it returns all posts with status future. If I swapped the order it would return only the published posts.

I set a break point and get the args being passed to WP_Query() and here they are:

Array
(
    [numberposts] => -1
    [category] => 0
    [orderby] => title
    [order] => ASC
    [include] => Array
        (
        )

    [exclude] => Array
        (
        )

    [meta_key] => 
    [meta_value] => 
    [post_type] => Array
        (
            [0] => my-type
            [1] => another-type
        )

    [suppress_filters] => 1
    [post_status] => Array
        (
            [0] => published
            [1] => future
        )

    [posts_per_page] => -1
    [ignore_sticky_posts] => 1
    [no_found_rows] => 1
)

I have seen examples where the status and type is passed as an array. What am I doing wrong?

I am running the following code:

$posts = get_posts([
    'post_type'   => ['my-type', 'another-type'],
    'numberposts' => -1,
    'order'       => 'ASC',
    'orderby'     => 'title',
    'post_status' => ['published','future']
]);

It is only returning the status that appears last in the array. So in the above example it returns all posts with status future. If I swapped the order it would return only the published posts.

I set a break point and get the args being passed to WP_Query() and here they are:

Array
(
    [numberposts] => -1
    [category] => 0
    [orderby] => title
    [order] => ASC
    [include] => Array
        (
        )

    [exclude] => Array
        (
        )

    [meta_key] => 
    [meta_value] => 
    [post_type] => Array
        (
            [0] => my-type
            [1] => another-type
        )

    [suppress_filters] => 1
    [post_status] => Array
        (
            [0] => published
            [1] => future
        )

    [posts_per_page] => -1
    [ignore_sticky_posts] => 1
    [no_found_rows] => 1
)

I have seen examples where the status and type is passed as an array. What am I doing wrong?

Share Improve this question edited Apr 18, 2020 at 4:30 Guerrilla asked Apr 18, 2020 at 4:17 GuerrillaGuerrilla 4156 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The correct post status for published posts is publish. Post statuses are in present tense (So publish, not published, and draft, not drafted).

发布评论

评论列表(0)

  1. 暂无评论