Skip to content

Instantly share code, notes, and snippets.

@ahgood
Created October 31, 2018 19:33
Show Gist options
  • Select an option

  • Save ahgood/3c542e2a291ca1cdebe32a7702ffd8cc to your computer and use it in GitHub Desktop.

Select an option

Save ahgood/3c542e2a291ca1cdebe32a7702ffd8cc to your computer and use it in GitHub Desktop.
Get post id from meta key and value
<?php
/**
* Get post id from meta key and value
* @param string $key
* @param mixed $value
* @return int|bool
* @author David M&aring;rtensson <[email protected]>
*/
function get_post_id_by_meta($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