-
-
Save tommcfarlin/d482b2383363558f5647f578e62d25c3 to your computer and use it in GitHub Desktop.
[WordPress] WordPress Queries with IN Clauses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, '","' ) . '"'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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