How to Write a WP_Query for Multiple Specific Post IDs

Recently, a client project I’m working on required something I don’t think I’ve done before: write a WP_Query to fetch a dynamically generated list of post IDs.

The accepted answer in this Stack Exchange article tells you all you need to know about the answer: the post__in (note the two underscores!) array key. Here’s the key bit that should be present in the $args array:

// Array of post IDs, generated however
$ids = array( 11, 63, 157 ); 

$args = array(
   'post__in' => $ids

$query = new WP_Query( $args );

The only thing I’ll reiterate is, as always, to make sure you use the post_type argument to input the post types you want to query for if you’re not just fetching posts—since WP_Query only looks for posts by default. If not, you’ll wonder why your well-written query is coming up empty.

Image credit: Andrew Beeston

Add a Comment

Your email address will not be published.