I can't seem to get drafts to show up with WP_Query, even when post_status is set to 'any' or 'draft'

        $args = array(
                    'p' => 1234, 
                    'post_type' => 'any',
                    'post_status' => 'any'
        $query = new WP_Query( $args );

        while ( $query->have_posts() ) : $query->the_post();
            // display the post


If I go back and Publish post 1234, WP_Query grabs it just fine. But when it is a draft, neither any nor draft grabs it.

I read that the exclude_from_search parameter in register_post_type() prevents posts from showing up with WP_Query, so I set it to false in the post_type I am grabbing from, like so:

$args = array(
    'labels' => $labels,
    'description' => '',
    'public' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'show_ui' => true,
    'menu_position' => 60,
    'menu_icon' => null,
    'capability_type' => post,
    'hierarchical' => false,
    'supports' => array('title', 'editor', 'author', 'excerpt', 'trackbacks', 'custom-fields', 'revisions'),
    'rewrite' => true,
    'query_var' => true,
    'can_export' => true,
    'show_in_nav_menus' => true,

register_post_type('chart', $args);

Is there something I'm overlooking here? I don't see why a draft post in this post type wouldn't show up in the query above.

  • what do you get if you print_r( $query )? I wonder if have_posts() or the_post() is preventing drafts from showing up... – mor7ifer Feb 16 '12 at 1:57
  • Here's the result of printing the query: pastebin.com/MpZK2vPf – supertrue Feb 16 '12 at 7:15
  • And that's a print_r() on $query?!? It looks like it's not querying...see my answer. – mor7ifer Feb 16 '12 at 10:35
  • Have you tried swapping p with post__in? – mrwweb May 23 '12 at 16:59
  • Your query-- the first one in your question-- works for me with a couple of different post types. I suspect that you have a plugin or something in your theme that is manipulating your queries in a way that is breaking this one. Disable your plugins and switch to the default theme and see what happens. – s_ha_dum Nov 4 '12 at 15:43

Try passing it as an array.

For example

$args = array(
    'p' => 1234, 
    'post_type' => 'any',
    'post_status' => array('draft')

Or for all types

$args = array(
    'p' => 1234, 
    'post_type' => 'any',
    'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash')
  • Unfortunately that didn't work. According to the codex and trac, post_status should accept a string, array, or comma-separated string equally well. – supertrue Feb 16 '12 at 0:55
  • I can confirm that this worked for me, when I need to get pending and drafts. Thanks – Jase Whatson Dec 10 '12 at 22:32
  • I just did a test with 4.7.2 with the 'post_type' as an array of 'any', and a text string of 'any' [so 'post_type' => array('any') 'post_type' => 'any' ] and both parameters were able to get draft posts – Rick Hellewell Feb 16 '17 at 22:49

I just came across this exact scenario. In my opinion it should work, but when I tried a combination of 'p' => 123 and 'post_status' => 'any', it would not return the post if the post status is draft.

I was able to work around this by using 'post__in' => array( 123 ) instead. So:

$args = array(
    'post__in' => array( 123 ),
    'post_type' => 'any',
    'post_status' => 'any',
$query = new WP_Query( $args );
// ... etc.

It looks like you might have an odd bug going on. Try replacing the oneline WP_Query with the following:

$query = new WP_Query();
$posts = $query->query( $args );
  • Should I then also change $query to $posts in $query->have_posts() and $query->the_post()? – supertrue Feb 16 '12 at 14:21
  • Yep, though you could name my $query to $my_query and my $posts to $query and it wouldn't make much difference. Basically the issue I'm trying to circumvent is the failure of the query to process...actually, where did you put that print_r() you did? – mor7ifer Feb 17 '12 at 1:46
  • I put it after WP_Query and before the loop, like this: $query = new WP_Query( $args ); print_r($query); while ( $query->have_posts() ) : $query->the_post();. – supertrue Feb 17 '12 at 18:44
  • Yea, try calling the query() method and see how that goes. – mor7ifer Feb 18 '12 at 4:54
  • Any solution to this? – TR3B Mar 7 '15 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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