WP_Query will not display draft posts

I'm trying to get WP_Query to display ALL posts in an array but only ones with status published are showing:

global $wp_query;

$ids = array(130, 132);
$args = array(
    'post_status' = array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'), 
     // 'post_status' = 'any', // same output
    'post__in' = $ids, 
    'post_type' = 'alpha'  
);

$q = new WP_Query($args);

foreach ($q-posts as $post) {
    echo $post-id;
}

However this displays post id's regardless of their status:

// post status
foreach ($ids as $id) {
    echo get_post_status($id);
}

This is fresh install of the Bones theme with no plugins. How do I display all posts in the array regardless of status? I must be missing something in the codex...

Topic post-status wp-query Wordpress

Category Web


You can just 'post_status' => 'any'.

Here is the completed code.

    global $wp_query;

    $ids   = array(130,132);
    $args  = array(
        'post_status' => 'any', 
        // 'post_status' => 'any', // same output
        'post__in' => $ids, 
        'post_type' => 'alpha'  
    );

    $q     = new WP_Query($args);

    foreach ($q->posts as $post) {
        echo $post->id;

    }

Official docs on post_status option https://developer.wordpress.org/reference/classes/wp_query/#status-parameters


Please try like this:

<?php

global $wp_query;

$ids   = array(130,132);
$args  = array(
            'post_status' => array(        
            'publish',                      
            'pending',                      
            'draft',                        
            'auto-draft',                   
            'future',                       
            'private',                     
            'inherit',                     
            ),
            'post__in' => $ids, 
            'post_type' => 'alpha'
        );
$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ){
    while ( $the_query->have_posts() ) : $the_query->the_post();
        echo get_the_ID();
    endwhile;
}

wp_reset_postdata();

?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.