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?

Was it helpful?

Solution

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 and array with all of the types you want:

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

while ( $loop->have_posts() ) : $loop->the_post();

OTHER TIPS

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; //...
}

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

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' => get_post_stati(['exclude_from_search' => false]),
]);

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',
 )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top