Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active June 20, 2017 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/d482b2383363558f5647f578e62d25c3 to your computer and use it in GitHub Desktop.
Save tommcfarlin/d482b2383363558f5647f578e62d25c3 to your computer and use it in GitHub Desktop.
[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