61

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?

4
  • 5
    Have you tried using the post_status parameter, ie. 'post_status' => 'any'?
    – t31os
    Mar 30, 2011 at 13:50
  • 2
    I strongly recommend using WP_Query pre_get_posts or get_posts instead of query_posts. Never use query_posts
    – Tom J Nowell
    Apr 16, 2013 at 12:45
  • @TomJNowell: that was way back :) I use WP_Query most ofter now..
    – Sisir
    Apr 17, 2013 at 17:22
  • 1
    @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).
    – Aurovrata
    Jan 30, 2017 at 8:10

6 Answers 6

110

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();
6
  • 9
    You could also use get_post_stati() to get all statuses, including custom ones.
    – fuxia
    Jan 31, 2013 at 11:52
  • 5
    A wasted opportunity to kill off a query_posts call...
    – Tom J Nowell
    Apr 16, 2013 at 12:45
  • 1
    @aequalsb what about 'post_status' => array_diff(get_post_stati(), ['inherit']);
    – Cheslab
    Oct 29, 2018 at 13:05
  • 5
    off-topic. 'any' is a real thing actually. Docs: developer.wordpress.org/reference/classes/wp_query/… Jan 20, 2020 at 14:26
  • 1
    This is the wrong answer. any is the right answer to the question How to Get All Posts with any post status?
    – user75665
    May 23, 2022 at 8:19
23

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; //...
}
4
  • 2
    $posts and $post conflict with Wordpress' own variable names. If you are using this code to put something in other than the primary (main content) div, this will overwrite what would have been shown in main. If your intention really is to completely replace the original query results, this is what you want, of course. But it's still a good idea to rename the $posts and $post variables. Feb 3, 2014 at 7:54
  • 6
    @Henrik i am not intending to diminish your comment at all (your logic is sound and safe), but i consider using $post/$posts as perfectly acceptable inside a function without access to the global $post/$posts variables -- because it helps me maintain logic during development.
    – aequalsb
    Jan 3, 2017 at 1:14
  • well.. almost - just try it vs @bainternet answer, you'll probably get a different count. it doesn't include auto-draft & trashed.
    – Sagive
    May 12, 2022 at 10:16
  • This is the right answer to the question How to Get All Posts with any post status?. any will get you ANY post type statūs.
    – user75665
    May 23, 2022 at 8:20
8

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',
 )
);
6

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).

5
  • 4
    From the WP_Query docs: 'any' - retrieves any status except those from post types with 'exclude_from_search' set to true. (There's a typo there, they actually mean post statuses instead of post types.) This means statuses auto-draft and trash are excluded.
    – Tamlyn
    Apr 15, 2013 at 12:50
  • @Tamlyn Afaik, this is no typo. It retrieves any status from post types that are publicly available. Status are just terms. They got no public or private property themselves. You could disable a taxonomy with disabling the query_var... for whatever reason one would do that. Sidenote: The plural of post status is....
    – kaiser
    Apr 15, 2013 at 14:30
  • 1
    If you trace through the code (often easier than reading the docs, I find) you can see that WP_Query#get_posts() calls get_post_stati() which filters $wp_post_statuses for values where exclude_from_search is true then it excludes posts with these statuses from the query. There's a similar process for post types when post_type is set to 'any'.
    – Tamlyn
    Apr 16, 2013 at 11:15
  • @Tamlyn After checking the contents of the $wp_post_statuses property, I have to admit that you're right :)
    – kaiser
    Apr 16, 2013 at 12:07
  • 1
    doesn't work for trash status. Dec 10, 2018 at 20:56
4

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])),
]);
1

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

1
  • 1
    Wellcome to WPSE! :)
    – Sisir
    Aug 23, 2021 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.