Anjum
Anjum

Reputation: 277

How to get post_id from post_meta

How can we get post_id from post_meta by given meta_key,

I want to get post_id from post_meta where meta_key is :

_wp_page_template

and meta_value is

my_page_template.php

Would like some help please.

Thanks

Upvotes: 2

Views: 1618

Answers (1)

Rochester Oliveira
Rochester Oliveira

Reputation: 1483

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

Upvotes: 5

Related Questions