{"id":55145,"date":"2021-10-03T21:10:05","date_gmt":"2021-10-03T19:10:05","guid":{"rendered":"https:\/\/samuelchopard.ch\/wiki\/?p=55145"},"modified":"2022-01-11T20:35:16","modified_gmt":"2022-01-11T19:35:16","slug":"create-shortcode-to-display-products-that-are-out-of-stock","status":"publish","type":"post","link":"https:\/\/samuelchopard.ch\/wiki\/library\/create-shortcode-to-display-products-that-are-out-of-stock\/","title":{"rendered":"Create shortcode to display products that are out of stock"},"content":{"rendered":"<p><!--more--><br \/>\nPaste the following code in functions.php:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">\/* Shortcode to display out of stock products *\/\r\nadd_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );\r\n   \r\nfunction bbloomer_out_of_stock_products_shortcode() {\r\n \r\n   $args = array(\r\n      'post_type' =&gt; 'product',\r\n      'posts_per_page' =&gt; -1, \/* \"-1\" to display all products *\/\r\n      'post_status' =&gt; 'publish',\r\n      'meta_query' =&gt; array(\r\n         array(\r\n            'key' =&gt; '_stock_status',\r\n            'value' =&gt; 'outofstock',\r\n         )\r\n      ),\r\n      'fields' =&gt; 'ids',\r\n   );\r\n    \r\n   $product_ids = get_posts( $args ); \r\n   $product_ids = implode( \",\", $product_ids );\r\n    \r\n    return do_shortcode(\"[products ids='$product_ids']\");\r\n}\r\n<\/pre>\n<p>Then, use the following shortcode to display the products:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">[out_of_stock_products]\r\n<\/pre>\n<p>Source: <a target=\"_blank\" href=\"https:\/\/www.businessbloomer.com\/woocommerce-display-stock-products-shortcode\/\">https:\/\/www.businessbloomer.com\/woocommerce-display-stock-products-shortcode\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Paste the following code in functions.php: \/* Shortcode to display out of stock products *\/ add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' ); function [...]","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[397,211,234,327],"class_list":["post-55145","post","type-post","status-publish","format-standard","hentry","category-woocommerce","tag-outofstock","tag-php","tag-products","tag-woocommerce"],"_links":{"self":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts\/55145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/comments?post=55145"}],"version-history":[{"count":3,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts\/55145\/revisions"}],"predecessor-version":[{"id":55157,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/posts\/55145\/revisions\/55157"}],"wp:attachment":[{"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/media?parent=55145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/categories?post=55145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samuelchopard.ch\/wiki\/wp-json\/wp\/v2\/tags?post=55145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}