How to Get All Posts with any post status?

  • I am creating a front end dashboard where I need to show all the posts by the current user. So, I need to show posts in all states, mainly published, trashed and the pending. I am now using a simple query but it is returning only the published posts.

    $query = array(
        'post_type' => 'my-post-type',
        'post_author' => $current_user->ID              
        );
        query_posts($query);
    

    Can anyone help? What else do I need to do?

    Have you tried using the `post_status` parameter, ie. `'post_status' => 'any'`?

    I ***strongly*** recommend using `WP_Query` `pre_get_posts` or `get_posts` instead of `query_posts`. Never use `query_posts`

    @TomJNowell: that was way back :) I use WP_Query most ofter now..

    @Sisir be careful, use `WP_Query` for front-end, and `get_posts` for admin queries as there is an issue with `wp_reset_postdata` (see the note and ticket on this issue).

  • Bainternet

    Bainternet Correct answer

    10 years ago

    You can use the post_status parameter:

    * 'publish' - a published post or page
    * 'pending' - post is pending review
    * 'draft' - a post in draft status
    * 'auto-draft' - a newly created post, with no content
    * 'future' - a post to publish in the future
    * 'private' - not visible to users who are not logged in
    * 'inherit' - a revision. see get_children.
    * 'trash' - post is in trashbin. added with Version 2.9. 
    

    I'm not sure that it accepts 'any' so use an array with all of the statuses you want:

    $args = array(
        'post_type' => 'my-post-type',
        'post_author' => $current_user->ID,
        'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash')    
    );
    $query = new WP_Query($args);
    
    while ( $query->have_posts() ) : $query->the_post();
    

    You could also use `get_post_stati()` to get all statuses, including custom ones.

    A wasted opportunity to kill off a `query_posts` call...

    too bad we can't do something like this `'post_status' => array( '!inherit' );` (to indicate any post_status other than inherit)

    @aequalsb what about `'post_status' => array_diff(get_post_stati(), ['inherit']);`

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM