[wp-hackers] How to get the meta_ID?

Oliver Schlöbe office at wpseek.com
Fri Jan 8 23:58:40 UTC 2010

There is no built-in function that I know of so here is my humble 
attempt to get the meta_id *by meta_key*:

function get_mid_by_key( $post_id, $meta_key ) {
     global $wpdb;
     $mid = $wpdb->get_var( $wpdb->prepare("SELECT meta_id FROM 
$wpdb->postmeta WHERE post_id = %d AND meta_key = %s", $post_id, 
$meta_key) );
     if( $mid != '' )
         return (int)$mid;

     return false;

Invoke the function like so:

get_mid_by_key( your_post_id, 'your_meta_key' );

Oliver Schlöbe


-- Code so clean you could eat off it.

On 08.01.2010 21:22, Matthew Gerring wrote:
> Is there an easy way to get the ID of a particular meta entry from the postmeta table? If not, what would be the hard way to do it?
> -Matthew
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list