{"id":6092,"date":"2022-08-13T11:17:31","date_gmt":"2022-08-13T15:17:31","guid":{"rendered":"https:\/\/themespress.ca\/?p=6092"},"modified":"2022-10-01T14:21:18","modified_gmt":"2022-10-01T18:21:18","slug":"display-count-attachements-post-type","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/display-count-attachements-post-type\/","title":{"rendered":"Count of attached files &#8211; Post Type"},"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>In this tutorial, we will display the count of attached media files like images corresponding to a post type.<\/p>\n<p>So, we will create a shortcode to display the media count. We need to make a <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_query\/\" target=\"_blank\" rel=\"noopener\">wp query<\/a>.<\/p>\n<p>You can read our article about the <a href=\"https:\/\/themespress.ca\/creer-un-shortcode-query-post-wordpress\/\" target=\"_blank\" rel=\"noopener\">WP Query shortcode.<\/a><\/p>\n<p>Bill Erickson offers us the whole list of arguments we can make in our <a href=\"https:\/\/www.billerickson.net\/code\/wp_query-arguments\/\" target=\"_blank\" rel=\"noopener\">WP Query (Arguments)<\/a><\/p>\n<p>Knowing the name parameter of your post type, you will, therefore, set up this shortcode.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Shortcode to display the count of the attached files - Post Type<\/h2><div id=\"ts-enlighterjs-container-7510538\" 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-7510538\" 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=\"\">add_shortcode( &#039;addocountattachementsposttype&#039;, &#039;addo_count_attachementsposttype&#039; );\nfunction addo_count_attachementsposttype(){\t\n\tglobal $post;\n    \/\/Get all attachments\n    $attachments = get_posts( array(\n        &#039;post_type&#039; =&gt; &#039;attachment&#039;,\n        &#039;posts_per_page&#039; =&gt; -1\n    ) );\n\n    $att_count = 0;\n    if ( $attachments ) {\n        foreach ( $attachments as $attachment ) {\n            \/\/ Check for the post type based on inpidual attachment&#039;s parent\n            if ( &#039;crasyphotosgalleries&#039; == get_post_type($attachment-&gt;post_parent) ) {\n                $att_count = $att_count + 1;\n            }\n        }\n        echo $att_count;\n    }\n}\n<\/pre><\/div><div class=\"vc_row wpb_row vc_inner vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-6\"><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>Then, replace <em>crasyphotosgalleries<\/em> with the name of your post type.<\/p>\n<p>Here in our example (addocountattachmentsposttype) you can change the name of your WordPress shortcode.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-3072121\" 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-3072121\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"standard\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">[addocountattachementsposttype]<\/pre><\/div><\/div><\/div><\/div><div class=\"wpb_column vc_column_container vc_col-sm-6\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div  class=\"wpb_single_image wpb_content_element vc_align_left wpb_content_element\">\n\t\t\n\t\t<figure class=\"wpb_wrapper vc_figure\">\n\t\t\t<div class=\"vc_single_image-wrapper   vc_box_border_grey\"><img decoding=\"async\" width=\"1000\" height=\"675\" src=\"https:\/\/themespress.ca\/wp-content\/uploads\/2022\/08\/Count-of-attached-files-Post-Type.jpg\" class=\"vc_single_image-img attachment-full\" alt=\"Count of attached files - Post Type\" title=\"Count of attached files - Post Type\" srcset=\"https:\/\/themespress.ca\/wp-content\/uploads\/2022\/08\/Count-of-attached-files-Post-Type.jpg 1000w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/08\/Count-of-attached-files-Post-Type-600x405.jpg 600w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/08\/Count-of-attached-files-Post-Type-300x203.jpg 300w, https:\/\/themespress.ca\/wp-content\/uploads\/2022\/08\/Count-of-attached-files-Post-Type-768x518.jpg 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/div>\n\t\t<\/figure>\n\t<\/div>\n<\/div><\/div><\/div><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"In this tutorial, we will display the count of attached media files like images corresponding to a post type. So, we will create a shortcode to display the media count.<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/display-count-attachements-post-type\/\">Read More<\/a><\/div>","protected":false},"author":1,"featured_media":6077,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[418],"class_list":["post-6092","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-wordpress-shortcodes"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6092","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=6092"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6092\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/6077"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}