{"id":6193,"date":"2017-03-30T12:48:01","date_gmt":"2017-03-30T16:48:01","guid":{"rendered":"https:\/\/themespress.ca\/?p=6193"},"modified":"2022-09-03T12:52:14","modified_gmt":"2022-09-03T16:52:14","slug":"count-post-meta-key-by-wordpress-post-id","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/count-post-meta-key-by-wordpress-post-id\/","title":{"rendered":"Count Post meta key by WordPress Post ID"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial, we will create a query that will count the number of <strong>post meta key<\/strong>.<\/p>\n<p>Let&#8217;s say we need to create a post meta views that will count the number of views per post ID.<\/p>\n<p>First of all, we need to request the data used by fetching the meta key with get_post_meta.<\/p>\n<p>With the example below, we have the post meta key called _post_like_count which will count the number of likes for each WordPress post.<\/p>\n<p>Here&#8217;s the code snippet to inject into your single.php or custom single post type template.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-9691188\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-9691188\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php \n$podcast_date = get_post_meta(get_the_ID(), &#039;_post_like_count&#039;, true); \n$count = $podcast_date; \necho $count; \nif ($count &gt; 1) { \necho &#039;View(s)&#039;; \n} \nelse { \necho &#039;view&#039;; \n} \n?&gt;<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Feels free to use our <strong>Via Post Meta Key<\/strong> widget which will automatically sort your posts by <strong>post meta key<\/strong>.<br \/>\n<a class=\"more\" href=\"https:\/\/themespress.ca\/produits\/widgets\/widget-via-like-post\/\" target=\"_blank\" rel=\"noopener\">Via Post Meta Key Widget<\/a><\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will create a query that will count the number of post meta key. Let's say we need to create a post meta views that will count<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/count-post-meta-key-by-wordpress-post-id\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4624,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[318,330],"class_list":["post-6193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-functions","tag-posts"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=6193"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6193\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4624"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}