{"id":80,"date":"2010-01-09T20:09:04","date_gmt":"2010-01-09T19:09:04","guid":{"rendered":"http:\/\/wpseek.com\/blog\/?p=80"},"modified":"2022-03-07T09:40:20","modified_gmt":"2022-03-07T08:40:20","slug":"how-to-get-the-meta-id-by-meta-key","status":"publish","type":"post","link":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/","title":{"rendered":"How to get the Meta ID by Meta Key"},"content":{"rendered":"<h4>The problem<\/h4>\n<p>Well, I guess it&#8217;s not too often that you need to get the ID of a particular meta entry from the postmeta table in WordPress. However, Matthew on the wp-hackers mailing list <a href=\"http:\/\/lists.automattic.com\/pipermail\/wp-hackers\/2010-January\/029798.html\" target=\"_blank\" rel=\"noopener\">needed exactly that<\/a>.<!--more--><\/p>\n<h4>The solution<\/h4>\n<p>So here&#8217;s a function to get the ID of a meta entry based on the given meta_key:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">function get_mid_by_key( $post_id, $meta_key ) {\r\n  global $wpdb;\r\n  $mid = $wpdb-&gt;get_var( $wpdb-&gt;prepare(&quot;SELECT meta_id FROM $wpdb-&gt;postmeta WHERE post_id = %d AND meta_key = %s&quot;, $post_id, $meta_key) );\r\n  if( $mid != '' )\r\n    return (int)$mid;\r\n\r\n  return false;\r\n}<\/pre>\n<h4>What does it do?<\/h4>\n<p>The function returns the ID of the meta entry in the postmeta table or <em>false<\/em> if no results are returned. Invoke the function like so:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">get_mid_by_key( your_post_id, 'your_meta_key' );<\/pre>\n<h4>Requirements<\/h4>\n<ul>\n<li>WordPress 2.3 and above<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The problem Well, I guess it&#8217;s not too often that you need to get the ID of a particular meta entry from the postmeta table in WordPress. However, Matthew on the wp-hackers mailing list needed exactly that.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[28,18,25,26,27,29],"class_list":["post-80","post","type-post","status-publish","format-standard","hentry","category-wordpress-hacks","tag-function","tag-id","tag-meta","tag-meta_id","tag-meta_key","tag-wpdb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to get the Meta ID by Meta Key &#8211; wpseek.com \/\/ blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to get the Meta ID by Meta Key &#8211; wpseek.com \/\/ blog\" \/>\n<meta property=\"og:description\" content=\"The problem Well, I guess it&#8217;s not too often that you need to get the ID of a particular meta entry from the postmeta table in WordPress. However, Matthew on the wp-hackers mailing list needed exactly that.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/\" \/>\n<meta property=\"og:site_name\" content=\"wpseek.com \/\/ blog\" \/>\n<meta property=\"article:published_time\" content=\"2010-01-09T19:09:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-07T08:40:20+00:00\" \/>\n<meta name=\"author\" content=\"wpseek (admin)\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"wpseek (admin)\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2010\\\/how-to-get-the-meta-id-by-meta-key\\\/80\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2010\\\/how-to-get-the-meta-id-by-meta-key\\\/80\\\/\"},\"author\":{\"name\":\"wpseek (admin)\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\"},\"headline\":\"How to get the Meta ID by Meta Key\",\"datePublished\":\"2010-01-09T19:09:04+00:00\",\"dateModified\":\"2022-03-07T08:40:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2010\\\/how-to-get-the-meta-id-by-meta-key\\\/80\\\/\"},\"wordCount\":162,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\"},\"keywords\":[\"function\",\"ID\",\"meta\",\"meta_id\",\"meta_key\",\"wpdb\"],\"articleSection\":[\"WordPress Hacks\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/wpseek.com\\\/blog\\\/2010\\\/how-to-get-the-meta-id-by-meta-key\\\/80\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2010\\\/how-to-get-the-meta-id-by-meta-key\\\/80\\\/\",\"url\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2010\\\/how-to-get-the-meta-id-by-meta-key\\\/80\\\/\",\"name\":\"How to get the Meta ID by Meta Key &#8211; wpseek.com \\\/\\\/ blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#website\"},\"datePublished\":\"2010-01-09T19:09:04+00:00\",\"dateModified\":\"2022-03-07T08:40:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2010\\\/how-to-get-the-meta-id-by-meta-key\\\/80\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wpseek.com\\\/blog\\\/2010\\\/how-to-get-the-meta-id-by-meta-key\\\/80\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/2010\\\/how-to-get-the-meta-id-by-meta-key\\\/80\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to get the Meta ID by Meta Key\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/\",\"name\":\"wpseek.com \\\/\\\/ blog\",\"description\":\"A random blog about WordPress\",\"publisher\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/d19081ecaffc56ccc034dbd009ede8eb\",\"name\":\"wpseek (admin)\",\"logo\":{\"@id\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"http:\\\/\\\/wpseek.com\"],\"url\":\"https:\\\/\\\/wpseek.com\\\/blog\\\/author\\\/wpseek\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to get the Meta ID by Meta Key &#8211; wpseek.com \/\/ blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/","og_locale":"en_US","og_type":"article","og_title":"How to get the Meta ID by Meta Key &#8211; wpseek.com \/\/ blog","og_description":"The problem Well, I guess it&#8217;s not too often that you need to get the ID of a particular meta entry from the postmeta table in WordPress. However, Matthew on the wp-hackers mailing list needed exactly that.","og_url":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/","og_site_name":"wpseek.com \/\/ blog","article_published_time":"2010-01-09T19:09:04+00:00","article_modified_time":"2022-03-07T08:40:20+00:00","author":"wpseek (admin)","twitter_card":"summary_large_image","twitter_misc":{"Written by":"wpseek (admin)","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/#article","isPartOf":{"@id":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/"},"author":{"name":"wpseek (admin)","@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb"},"headline":"How to get the Meta ID by Meta Key","datePublished":"2010-01-09T19:09:04+00:00","dateModified":"2022-03-07T08:40:20+00:00","mainEntityOfPage":{"@id":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/"},"wordCount":162,"commentCount":0,"publisher":{"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb"},"keywords":["function","ID","meta","meta_id","meta_key","wpdb"],"articleSection":["WordPress Hacks"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/","url":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/","name":"How to get the Meta ID by Meta Key &#8211; wpseek.com \/\/ blog","isPartOf":{"@id":"https:\/\/wpseek.com\/blog\/#website"},"datePublished":"2010-01-09T19:09:04+00:00","dateModified":"2022-03-07T08:40:20+00:00","breadcrumb":{"@id":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wpseek.com\/blog\/2010\/how-to-get-the-meta-id-by-meta-key\/80\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/wpseek.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to get the Meta ID by Meta Key"}]},{"@type":"WebSite","@id":"https:\/\/wpseek.com\/blog\/#website","url":"https:\/\/wpseek.com\/blog\/","name":"wpseek.com \/\/ blog","description":"A random blog about WordPress","publisher":{"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wpseek.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/d19081ecaffc56ccc034dbd009ede8eb","name":"wpseek (admin)","logo":{"@id":"https:\/\/wpseek.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/wpseek.com"],"url":"https:\/\/wpseek.com\/blog\/author\/wpseek\/"}]}},"_links":{"self":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts\/80","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/comments?post=80"}],"version-history":[{"count":2,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts\/80\/revisions"}],"predecessor-version":[{"id":12771,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/posts\/80\/revisions\/12771"}],"wp:attachment":[{"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/media?parent=80"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/categories?post=80"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpseek.com\/blog\/wp-json\/wp\/v2\/tags?post=80"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}