Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i am creating a frontend dashboard where i need to show all the post by the current user. So i need to show posts with all post status. Mainly published, trashed and the pending. I am now using simple query post but it is returning only the published posts as expected.

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

Can anyone help what i need do extra?

share|improve this question
4  
Have you tried using the post_status parameter, ie. 'post_status' => 'any'? –  t31os Mar 30 '11 at 13:50
1  
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 '13 at 12:45
    
@TomJNowell: that was way back :) I use WP_Query most ofter now.. –  Sisir Apr 17 '13 at 17:22

3 Answers 3

up vote 12 down vote accepted

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')    
);
query_posts($query);
share|improve this answer
    
yes.. got it :) tnx –  Sisir Mar 31 '11 at 16:03
1  
You could also use get_post_stati() to get all statuses, including custom ones. –  toscho Jan 31 '13 at 11:52
1  
A wasted opportunity to kill off a query_posts call... –  Tom J Nowell Apr 16 '13 at 12:45

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

share|improve this answer
    
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 '13 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 '13 at 14:30
    
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 '13 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 '13 at 12:07

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; //...
}
share|improve this answer
    
$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. –  Henrik Erlandsson Feb 3 at 7:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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