Get All IDs Of A Post Type Using WP_Query

I am trying to get a list of a custom post type’s IDs using WP_Query, but it is returning undesired result, which is a memory leak and stuck browser.

Here is the code I use:

    $the_query = new WP_Query("post_type=post&posts_per_page=-1&field=ids");    
    if ($the_query->have_posts()) {
      while ($the_query->have_posts()){
         echo get_the_ID();
      }
    }

It makes my browser infinitely trying to load the page. May be somebody know what’s wrong with the code above..

2 Answers
2

You are missing the the_post() function call in the loop. Just add

$the_query->the_post();

in your loop. Apart from that, your loop should work

EDIT

You should also not forget to reset your postdata after the query is done

Leave a Comment