Skip to content

Instantly share code, notes, and snippets.

@RiFi2k
Created April 5, 2017 00:12
Show Gist options
  • Save RiFi2k/8d0e4b7a5363bd09d86e3f3e7cd6d6d4 to your computer and use it in GitHub Desktop.
Save RiFi2k/8d0e4b7a5363bd09d86e3f3e7cd6d6d4 to your computer and use it in GitHub Desktop.
if (!function_exists('get_post_id_by_meta_key_and_value')) {
function get_post_id_by_meta_key_and_value($key, $value) {
global $wpdb;
$meta = $wpdb->get_results("SELECT * FROM `".$wpdb->postmeta."` WHERE meta_key='".$wpdb->escape($key)."' AND meta_value='".$wpdb->escape($value)."'");
if (is_array($meta) && !empty($meta) && isset($meta[0])) {
$meta = $meta[0];
}
if (is_object($meta)) {
return $meta->post_id;
}
else {
return false;
}
}
}
// $postID = get_post_id_by_meta_key_and_value('hobby', 'writing');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment