0

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...

  • Probably the same problem as here: wordpress.stackexchange.com/questions/176949/… – fuxia Mar 8 '15 at 5:31
  • Did the link from @toscho not work. It would be nice if you leave some feedback if you need extra help. :-) – Pieter Goosen Mar 9 '15 at 13:11
  • Thank you @PieterGoosen for that nice comment, I have not gotten to testing those solutions because of day work. However I am not utilizing a custom status... I'll be sure to report – Ben Racicot Mar 9 '15 at 17:16
0

Try using 'post_status' => 'any'

  • well it does answer your question. You asked how to show all posts not just published. by changing the post_status to any instead of an array it will show all types of posts including draft. Also if you're using wp_query try using the wordpress while loop instead. while ( $q->have_posts() ) {$q->the_post(); //do stuff}. – dgas02 Mar 8 '15 at 16:06
0

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

?>
0

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;

    }
-1

Try:

$args  = array('post_status' => 'any', 'post__in' => $ids, 'post_type' => 'alpha', 'posts_per_page' => -1);
  • Please add explanation of your code. – Nilambar Mar 9 '15 at 5:14

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.