How do you use prepare when asking for a list of id’s

$wpdb->prepare() use the same syntax for formatting like php’s printf(). SSo you will need something like ... WHERE post_id IN (%d,%d,%d) ...

This will fit for three ids. But what if we got more or less than three ids? We will use the php tools to create the needed formatting string (assuming $ids is an array with the ids):

Count the IDs

count( $ids )

Create a string with a ‘%d,’ for every ID in $ids

str_repeat( '%d,', count( $ids ) )

Now we have a string like %d,%d,%d,. There is one comma to much at the end. Let’s remove it

$ids_format_string = rtrim( str_repeat( '%d,', count( $ids ) ), ',' );

And use this in this way '... WHERE post_id IN (' . $ids_format_string . ') ...'