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 :

Read More

_wp_page_template

and meta_value is

my_page_template.php

Would like some help please.

Thanks

Related posts

Leave a Reply

1 comment

  1. 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