Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin tommcfarlin/00-keys-to-map.php Secret
Last active Jun 20, 2017

Embed
What would you like to do?
[WordPress] WordPress Queries with IN Clauses
<?php
// This is used to maintain a map of data should we need to add more.
$data_types = [
'data_item_one',
'data_item_two',
'...'
'data_item_ten,
];
<?php
/**
* Converts the incoming array into a comma-delimited string with
* quotes wrapped around each key.
*
* @access private
*
* @param array $arr The array to convert to a string.
* @return string The string representation of the array delimited by quotes and commas.
*/
private function convert_to_sql_ready_string( $arr ) {
return '"' . implode( $arr, '","' ) . '"';
}
<?php
public function get_data_values() {
global $wpdb;
$query = "
SELECT post_id, meta_key, meta_value
FROM $wpdb->postmeta WHERE
meta_key in ( $this->data_types )
AND
meta_value <> '';
";
$results = $wpdb->get_results( $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.