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 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
        endwhile;

        wp_reset_postdata();

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.

share|improve this question
    
what do you get if you print_r( $query )? I wonder if have_posts() or the_post() is preventing drafts from showing up... –  m0r7if3r 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. –  m0r7if3r 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

2 Answers 2

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')
);
share|improve this answer
    
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 –  nullptr Dec 10 '12 at 22:32

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 );
share|improve this answer
    
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? –  m0r7if3r 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. –  m0r7if3r Feb 18 '12 at 4: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.