Skip to content

Instantly share code, notes, and snippets.

@tradesouthwest
Last active November 12, 2018 17:52
Show Gist options
  • Save tradesouthwest/d91aa5262a00a263c61386c03391d3a5 to your computer and use it in GitHub Desktop.
Save tradesouthwest/d91aa5262a00a263c61386c03391d3a5 to your computer and use it in GitHub Desktop.
Retrieve post id from any meta field
<?php
//From: https://github.com/oyina/Note-Taker/blob/master/note.php
//Retreives Post Id from Meta Key
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment