How to Get All Posts with any post status?

The question:

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?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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

Method 2

There is simple way, how to get all posts with any status:

$articles = get_posts(
 array(
  'numberposts' => -1,
  'post_status' => 'any',
  'post_type' => get_post_types('', 'names'),
 )
);

Now you can iterate throughout all posts:

foreach ($articles as $article) { 
 echo $article->ID . PHP_EOL; //...
}

Method 3

In most cases you can use get_posts() with 'any' parameter for this:

$posts = get_posts(
 array(
  'numberposts' => -1,
  'post_status' => 'any',
  'post_type' => 'my-post-type',
 )
);

But this way you won’t get posts with status trash and auto-draft. You need to provide them explicitly, like this:

$posts = get_posts(
 array(
  'numberposts' => -1,
  'post_status' => 'any, trash, auto-draft',
  'post_type' => 'my-post-type',
 )
);

Or you can use get_post_stati() function to provide all existing statuses explicitly:

$posts = get_posts(
 array(
  'numberposts' => -1,
  'post_status' => get_post_stati(),
  'post_type' => 'my-post-type',
 )
);

Method 4

The WP_Query class method ->query() accepts an any argument for post_status. See wp_get_associated_nav_menu_items() for a proof.

The same goes for get_posts() (which is just a wrapper for above call).

Method 5

Even if you pass any as post_status, you still will not get the post in the result if all of the following conditions are true:

  1. A single post is being queried. An example of this would be querying by name, i.e. the slug.
  2. The post has a post status that is not public.
  3. The client does not have an active admin session, i.e. you are not currently logged in.

Solution

Query explicitly for every status. For example, to query for stati which are not trash or auto-draft (it’s pretty unlikely that you want those), you could do something like this:

$q = new WP_Query([
    /* ... */
    'post_status' => array_values(get_post_stati(['exclude_from_search' => false])),
]);

Method 6

Since I can’t yet comment: $args['post_status']='any'; works for ‘publish’ and ‘draft’, but not for ‘trash’, I needed $args['post_status']=array('any','trash');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment