How To Get WordPress Posts From A List Of IDs

In a website I recently built, I needed to create a WordPress loop of posts based on a list of IDs that I had.

How I arrived at that list of IDs is not important for this tutorial, but basically I did a geographical search of profiles based on a set distance from the starting point.

So I had an array of post IDs, but how do you get the WordPress posts from a list of IDs? Put the array of IDs into $args['post__in']. Then use that argument with any additional arguments in either WP_Query($args) or get_posts($args). It's important to have a double underscore between post and in.

Your choice on WP_Query($args) or get_posts($args) depends on how you want to use it. If you're doing the usual WordPress Loop with these posts, use WP_Query($args). The get_posts($args) will give you an array of the parts of the posts, which you'd probably loop with foreach.

Another reason you might want to use get_posts($args) is if your list of IDs is a comma separated string, rather than an array.

One of the downsides of using get_posts($args) is that you can't easily do pagination. All the posts that match the array of IDs you give will be brought into the query. If you use WP_Query($args) with Pagination and posts_per_page, then it will only fetch the posts for that page. On a high traffic website, or if you're likely to have a lot of posts in the query, this could cause some speed issues.

Get The WordPress Posts With WP_Query

I decided to use the usual WordPress Loop with my posts, so went with WP_Query($args). The code I used looked like this:

$post_ids_fetched = array(123, 456, 789);
$args = array (
  'post__in' => $post_ids_fetched,
  'orderby' => 'post__in',
  'post_status' => 'publish'
  'post_type' => 'profile'
);

$profiles = new WP_Query($job_args);

if($profiles->have_posts()) {
 while($profiles->have_posts()) {
  $profiles->the_post();
  // code to display the post
 }
}

You would put this into the template page in your child theme where you wanted this custom query displayed.

$post_ids_fetched is the array of post IDs that I'd gathered with the search function. This was a dynamically generated array, but I've put in an example array for this tutorial.

The post IDs were already in order of distance, so I wanted to preserve the order. To make sure that happened, I ordered by 'post__in'.

The 'post_type' was the custom post type 'profile'. This could be your own custom post type or post or page, depending on your situation.

I also had some pagination, but didn't include it here, so as not to distract from the essential workings.

Finally we run the query and the WordPress Loop.

Get The Posts With get_posts

If you wanted to use get_posts($args) instead, you'd keep the arguments the same and change from $profiles down. So it might look like this:

$post_ids_fetched = array(123, 456, 789);
$args = array (
  'post__in' => $post_ids_fetched,
  'orderby' => 'post__in',
  'post_status' => 'publish'
  'post_type' => 'profile'
);

$profiles = get_posts($job_args);

foreach ($profiles as $profile) {
 // code to display the post
}

The result will be an array of objects from the wp_posts database table. It will have every column from that table. As an example, the way to get the ID for a post inside the foreach loop would be:

$post_id = $profile->ID;

And the same applies to any column in the wp_posts database table for that entry.

If your $post_ids_fetched is a comma separated string, rather than an array, replace

'post__in' => $post_ids_fetched,

with

'include' => $post_ids_fetched,

That will allow you to automatically use wp_parse_id_list for the heavy lifting.

Mike Haydon

Thanks for checking out my WordPress and coding tutorials. If you've found these tutorials useful, why not consider supporting my work?

Buy me a coffee

Leave a Comment