WordPress WP_Query for multiple IDs returns null

I have following array:

array (12,15,21,32,33);

And then I use the following query to get the posts (of the above of IDs):

Read More
$the_query = new WP_Query( array("post__in"=>$ids_array) );       // edited

    while($the_query->have_posts())
    {
$the_query->the_post(); // added on edit, but still does not work      
the_title()."<br />";
    }

But I get nothing, no error and no interruption. I have checked the IDs and they are correct.

EDIT: I have put this query at the end of a module which is loaded into the footer. I don’t know if it is important or not:

Related posts

Leave a Reply

1 comment

  1. You forgot to add while ( $the_query->have_posts() ) : $the_query->the_post();.

    You are just checking to see if you have posts, but doing nothing further

    $ids_array = array (12,15,21,32,33);
    
    $the_query = new WP_Query( array('post__in'=>$ids_array) );       
    
    <?php if ( $the_query->have_posts() ) : ?>
    
      <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2></br>
      <?php
         endwhile; 
        wp_reset_postdata(); 
       endif; 
      ?>
    

    EDIT

    I think the underlying problem here is not this custom query, as this custom query works. It seems from your comments that you are using another custom query on the same page.

    I don’t know what the code of the first query looks like, but here are some troubleshooting points you need to go and have a look at

    • Most probably you did not reset the postdata on your first query. This will break your second query. wp_reset_postdata is extremely important when you are running custom queries with WP_Query and with get_posts. Check that you have used wp_reset_postdata after your first instance of WP_Query. Your first query should be in the same format as the one in my answer

    • You should use different variables for each instance of WP_Query. For example $variable1 = new WP_Query( YOUR ARGUMENTS ) for first instance and $variable2 = new WP_Query( YOUR ARGUMENTS ) for your second instance