{"id":6196,"date":"2017-03-28T13:51:42","date_gmt":"2017-03-28T17:51:42","guid":{"rendered":"https:\/\/themespress.ca\/?p=6196"},"modified":"2022-09-03T13:04:55","modified_gmt":"2022-09-03T17:04:55","slug":"woocommerce-products-sorted-by-sales","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/woocommerce-products-sorted-by-sales\/","title":{"rendered":"Products Sorted by Sales WooCommerce Plugin"},"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 function allowing you to display your best selling WooCommerce products. This query will include arguments that will check the WooCommerce <strong>&#8220;product&#8221;<\/strong> post type and use the meta query of the <strong>&#8220;total_sales&#8221;<\/strong> key.<\/p>\n<p>We increment $i inside the loop so the sorting number of each result starts at 1. We also need the WooCommerce variable $product in the loop so it returns the products.<\/p>\n<p>We want to display our WooCommerce products sorted by sales by making sure that the <strong>&#8220;total_sales&#8221;<\/strong> meta query key is superior to 0.<\/p>\n<p>We also need another condition that specifies if $units_sold > 1 we need to use plural. Otherwise, it&#8217;s singular.<\/p>\n<p>Here&#8217;s the code snippet to add into your WordPress plugin.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-1427527\" 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-1427527\" 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$i = 1; $args = array( \n&#039;post_type&#039;       =&gt; &#039;product&#039;,\n&#039;meta_key&#039;        =&gt; &#039;total_sales&#039;,\n&#039;orderby&#039;         =&gt; &#039;meta_value_num&#039;,\n&#039;posts_per_page&#039;  =&gt; 25,\n&#039;meta_query&#039;      =&gt; array(\n  array(\n    &#039;key&#039;      =&gt; &#039;total_sales&#039;,\n    &#039;value&#039;    =&gt; 0,\n    &#039;compare&#039;  =&gt; &#039;&gt;&#039;\n    )\n  )\n);\n\n$loop = new WP_Query( $args );\nwhile ( $loop-&gt;have_posts() ) : $loop-&gt;the_post(); \n?&gt;\n&lt;h3&gt;\n&lt;a id=&quot;&lt;?php the_id(); ?&gt;&quot; style=&quot;text-decoration: none;&quot; title=&quot;&lt;?php the_title(); ?&gt;&quot; href=&quot;&lt;?php the_permalink(); ?&gt;&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;\n     &lt;?php echo $i; ?&gt;&lt;?php the_title(); ?&gt;\n&lt;\/a&gt;\n&lt;\/h3&gt;\n&lt;?php $i++; endwhile; ?&gt;\n&lt;?php wp_reset_query(); ?&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>This query also lets you edit your HTML and CSS.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"This tutorial will show you how to create a function allowing you to display your best selling WooCommerce products. This query will include arguments that will check the WooCommerce \"product\"<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/woocommerce-products-sorted-by-sales\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4623,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[],"class_list":["post-6196","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\/6196","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=6196"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/6196\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4623"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=6196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=6196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=6196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}