How To Get The Post ID By Post Meta Value
September 10, 2018
sometimes we need to find post id by post meta key & value. below is function that really helps to find that post ID by meta key & value.
put this function in your function.php file :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function webtechsource_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='".esc_sql($value)."'"); if (is_array($meta) && !empty($meta) && isset($meta[0])) { $meta = $meta[0]; } if (is_object($meta)) { return $meta->post_id; }else { return false; } } |
How to use this function :
you call this function and pass the meta key and meta value and its return the post id.
1 |
$id = webtechsource_get_post_id_by_meta_key_and_value('meta_key','meta_value'); |
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.
I have read some good stuff here. Definitely
worth bookmarking for revisiting. I wonder how a lot attempt
you set to create this kind of great informative web site