{"id":6252,"date":"2016-11-08T18:06:14","date_gmt":"2016-11-08T23:06:14","guid":{"rendered":"https:\/\/themespress.ca\/?p=6252"},"modified":"2022-09-06T19:20:03","modified_gmt":"2022-09-06T23:20:03","slug":"display-the-number-of-woocommerce-products","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/display-the-number-of-woocommerce-products\/","title":{"rendered":"Display the Number of WooCommerce Products"},"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>This tutorial will show you how to create a query to display all of your WooCommerce products.<\/p>\n<p>First a quick explaination of <strong><a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/found_posts\/\" target=\"_blank\" rel=\"noopener\">found_posts<\/a><\/strong> which can be seen in the query below. It will allow you to filter the number of products found and matching the Posts ou Post Types inside your arguments.<\/p>\n<p>For everything else, it&#8217;s a simple and basic query for WooCommerce. The post for WooCommerce products is simply called <strong>product<\/strong>.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >Display the Number of WooCommerce Products<\/h2><div id=\"ts-enlighterjs-container-6170749\" 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-6170749\" 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=\"\">function via_classement_woocommerce_products_count_total() {\n    $args = array( &#039;post_type&#039; =&gt; &#039;product&#039;, &#039;post_status&#039; =&gt; &#039;publish&#039;, &#039;posts_per_page&#039; =&gt; -1 );\n\t$products = new WP_Query( $args );\n\techo &#039;&lt;span class=&quot;numberCircle&quot;&gt;&#039;;\n\techo $products-&gt;found_posts;\n\techo &#039;&lt;\/span&gt;&#039;;\n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Here&#8217;s how we can avoid multiplying your functions if you want to use the same function but you want to display the number of product by status:<br \/>\nHere are the various <a href=\"https:\/\/codex.wordpress.org\/Post_Status\" target=\"_blank\" rel=\"noopener\">post_status<\/a> of your posts or Post Types:<br \/>\n&#8211; &#8216;publish&#8217; == Published<br \/>\n&#8211; &#8216;Future&#8217; == Scheduled<br \/>\n&#8211; &#8216;Draft&#8217; == Draft<br \/>\n&#8211; &#8216;Pending&#8217; == Pending Review<br \/>\n&#8211; &#8216;Private&#8217; == Private<br \/>\n&#8211; &#8216;Trash&#8217; == Deleted<\/p>\n<p>Now let&#8217;s build the previous function considering we want to display the number of products and their status.<\/p>\n<p>Here&#8217;s the function to add in your <strong>functions.php<\/strong> file. Note that we stored a  <strong>$statut<\/strong> variable that will allow us to fetch the value that we want to assign to it. Said value is display as such: <strong>&#8216;post_status&#8217; =&gt; $statut<\/strong>.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-6006536\" 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-6006536\" 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=\"\">function via_classement_woocommerce_products_count_total($statut) {\n    $args = array( &#039;post_type&#039; =&gt; &#039;product&#039;, &#039;post_status&#039; =&gt; $statut, &#039;posts_per_page&#039; =&gt; -1 );\n\t$products = new WP_Query( $args );\n\techo &#039;&lt;span class=&quot;numberCircle&quot;&gt;&#039;;\n\techo $products-&gt;found_posts;\n\techo &#039;&lt;\/span&gt;&#039;;\n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Now in the desired WordPress php  template file, display the function this way:<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-1063992\" 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-1063992\" 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=\"\">&lt;?php echo via_classement_woocommerce_products_count_total($statut = &#039;publish&#039;); ?&gt;<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"This tutorial will show you how to create a query to display all of your WooCommerce products. First a quick explaination of found_posts which can be seen in the query<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/display-the-number-of-woocommerce-products\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4559,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[322,328],"class_list":["post-6252","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-query","tag-woocommerce-config"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6252","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=6252"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6252\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4559"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}