Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
$wpdb->prepare for SQL IN statements
$data_types = [ 'data_item_one', 'data_item_two' ];
// Props to @hellofromtonya for this.
$data_type_placeholders = implode( ', ', array_fill( 0, count( $data_types ), '%s' ) );
global $wpdb;
$query = "
SELECT pm.post_id, pm.meta_key, pm.meta_value
FROM {$wpdb->postmeta} AS pm
WHERE pm.meta_key IN ( {$data_type_placeholders} )
AND pm.meta_value <> ''
$sql_query = $wpdb->prepare( $query, $data_types );
$results = $wpdb->get_results( $sql_query );
return $results;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.