Created
March 28, 2018 21:10
-
-
Save csymlstd/e11c1eda611518b41efa1ca4d95a6e59 to your computer and use it in GitHub Desktop.
get wordpress post id by post_meta key and value
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 | |
if (!function_exists('get_post_id_by_meta_key_and_value')) { | |
/** | |
* Get post id from meta key and value | |
* @param string $key | |
* @param mixed $value | |
* @return int|bool | |
* @author David Mårtensson <david.martensson@gmail.com> | |
*/ | |
function get_post_id_by_meta($key, $value) { | |
global $wpdb; | |
$meta = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM ".$wpdb->postmeta." WHERE meta_key=%s AND meta_value=%s", $key, $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