{"id":7347,"date":"2023-08-19T14:36:49","date_gmt":"2023-08-19T18:36:49","guid":{"rendered":"https:\/\/themespress.ca\/?p=7347"},"modified":"2023-08-20T09:59:23","modified_gmt":"2023-08-20T13:59:23","slug":"number-of-wordpress-media-query","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/number-of-wordpress-media-query\/","title":{"rendered":"Number of WordPress Media Query"},"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\"><h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Number of WordPress Media Query<\/h2>\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 return the number of media (i.e. images) that have been uploaded in WordPress.<\/p>\n<p>Let&#8217;s start with image type. In our query below, we will create an SQL query that will fetch the number of images uploaded today. <strong>Le post mime type<\/strong><\/p>\n<p>.<strong>Here&#8217;s the SQL query inside a shortcode:<\/strong><\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-6308944\" 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-6308944\" 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=\"\">\/**************************** Count des images telechargees aujourd&#039;hui ********************************\/\n\nfunction count_attachments_today() {\n    global $wpdb;\n\n    $today = getdate();\n    $year = $today[&#039;year&#039;];\n    $month = $today[&#039;mon&#039;];\n    $day = $today[&#039;mday&#039;];\n\n    $count = $wpdb-&gt;get_var(\n        $wpdb-&gt;prepare(\n            &quot;SELECT COUNT(ID) FROM $wpdb-&gt;posts\n            WHERE post_type = &#039;attachment&#039;\n            AND post_mime_type LIKE &#039;image%%&#039;\n            AND YEAR(post_date) = %d\n            AND MONTH(post_date) = %d\n            AND DAY(post_date) = %d&quot;,\n            $year, $month, $day\n        )\n    );\n\n    return &#039;&lt;div class=&quot;addo40px addocenter&quot;&gt;&#039; . $count . &#039;&lt;\/div&gt;&#039;;\n}\nadd_shortcode( &#039;attachmentcounttoday&#039;, &#039;count_attachments_today&#039; );<\/pre><\/div><h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Number of WordPress Media Query<\/h2>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>We will now do the same query, but this type for the video format. <strong>Once again, here&#8217;s the SQL query inside a shortcode:<\/strong><\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-8160586\" 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-8160586\" 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=\"\">\/**************************** Count des videos telechargees aujourd&#039;hui ********************************\/\n\nfunction count_videos_attachments_today() {\n    global $wpdb;\n\n    $today = getdate();\n    $year = $today[&#039;year&#039;];\n    $month = $today[&#039;mon&#039;];\n    $day = $today[&#039;mday&#039;];\n\n    $count = $wpdb-&gt;get_var(\n        $wpdb-&gt;prepare(\n            &quot;SELECT COUNT(ID) FROM $wpdb-&gt;posts\n            WHERE post_type = &#039;attachment&#039;\n            AND post_mime_type LIKE &#039;video%%&#039;\n            AND YEAR(post_date) = %d\n            AND MONTH(post_date) = %d\n            AND DAY(post_date) = %d&quot;,\n            $year, $month, $day\n        )\n    );\n\n    return &#039;&lt;div class=&quot;addo40px addocenter&quot;&gt;&#039; . $count . &#039;&lt;\/div&gt;&#039;;\n}\nadd_shortcode( &#039;videoattachmentcounttoday&#039;, &#039;count_videos_attachments_today&#039; );\n<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"Number of Wordpress Media Query With this tutorial, we will create a query that will return the number of media (i.e. images) that have been uploaded in Wordpress. Let's start<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/number-of-wordpress-media-query\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":7349,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[],"class_list":["post-7347","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/7347","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=7347"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/7347\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/7349"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=7347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=7347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=7347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}