I have made a custom post 'case studies' which has a number of posts.

I wish to get the IDs of each of these posts to manipulate the data, but despite looking at similar threads, I cannot piece together a way to achieve this. Something like;

foreach post oftype custom, get the id

I am making a function to display this information in the back end, here is the snippet below.

function display_meta_box( $case_study ) 
    if (in_array('case_studies', get_post_types()))
        ...get the IDs of all posts of type 'case_studies'...
    ...do other stuff with IDs...

2 Answers 2


You can get a simple array of CPT IDs by using get_posts with the fields parameter.

$all_post_ids = get_posts(array(
    'fields'          => 'ids',
    'posts_per_page'  => -1,
    'post_type' => 'case_studies'

Found the basis of the answer buried in the codex

$args = array( 'post_type' => 'case_studies');

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  • 2
    You don't need echo with the_title() and you can also use the_ID() instead of echo get_the_id().
    – birgire
    Oct 20, 2014 at 13:57
  • 'basis' - merely showing as an example
    – myol
    Oct 20, 2014 at 13:59
  • 1
    Notice the difference between the_* vs get_the_* functions. In most cases it's like echo vs return.
    – birgire
    Oct 20, 2014 at 14:06
  • Interestingly, for my case, I needed to use get_the_* to manipulate the information in the while loop.
    – myol
    Oct 20, 2014 at 14:30
  • 5
    set fields to ids in your query and you'll get back just an array of IDs, and will use significantly less memory. you also probably want to set posts_per_page to -1 to get all of your posts.
    – Milo
    Oct 20, 2014 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.