开发者

How to get post_id from post_meta

开发者 https://www.devze.com 2023-02-19 21:30 出处:网络
How can we get post_id from post_meta by given meta_key, I want to get post_id from post_meta where meta_ke开发者_开发知识库y is :

How can we get post_id from post_meta by given meta_key,

I want to get post_id from post_meta where meta_ke开发者_开发知识库y is :

_wp_page_template

and meta_value is

my_page_template.php

Would like some help please.

Thanks


Put this in your functions.php an then call it when you want

 function getIdFromMeta( $meta_key, $meta_value ) {
    global $wpdb;
    $pid = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_value = '$meta_value' AND meta_key = '$meta_key' ORDER BY post_id DESC") );
    if( $pid != '' )
        return $pid;
    else 
        return false;
}

Hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消