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?
You can use the post_status parameter:
I’m not sure that it accepts ‘any’ so use an array with all of the statuses you want:
There is simple way, how to get all posts with any status:
Now you can iterate throughout all posts:
In most cases you can use
get_posts()
with'any'
parameter for this:But this way you won’t get posts with status
trash
andauto-draft
. You need to provide them explicitly, like this:Or you can use get_post_stati() function to provide all existing statuses explicitly:
The
WP_Query
class method->query()
accepts anany
argument forpost_status
. Seewp_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
aspost_status
, you still will not get the post in the result if all of the following conditions are true:name
, i.e. the slug.Solution
Query explicitly for every status. For example, to query for stati which are not
trash
orauto-draft
(it’s pretty unlikely that you want those), you could do something like this: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');