Created
April 20, 2020 09:28
-
-
Save sumitpore/64d3d659f40aa913861bf93448c1a823 to your computer and use it in GitHub Desktop.
Array Escape in WordPress. To be used with 'IN' keyword
This file contains hidden or 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
/** | |
* Format Array to make useful in SQL Queries | |
* | |
* @param array $array Array to be escaped. | |
* @return string | |
*/ | |
function escape_array( $array = array() ) { | |
global $wpdb; | |
$escaped = array(); | |
foreach ( $array as $k => $v ) { | |
if ( is_numeric( $v ) ) { | |
$escaped[] = $wpdb->prepare( '%d', $v ); | |
} else { | |
$escaped[] = $wpdb->prepare( '%s', $v ); | |
} | |
} | |
return implode( ',', $escaped ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment