TransWikia.com

WP_Query will not display draft posts

WordPress Development Asked by Ben Racicot on December 11, 2020

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…

4 Answers

You can just use '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;

    }

Answered by Liam Stewart on December 11, 2020

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();

?>

Answered by Jignesh Patel on December 11, 2020

Try:

$args  = array('post_status' => 'any', 'post__in' => $ids, 'post_type' => 'alpha', 'posts_per_page' => -1);

Answered by Marcio Dias on December 11, 2020

Try using 'post_status' => 'any'

Answered by dgas02 on December 11, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP