Web Development Blog

Woocommerce Shortcode to Display All Products

Here is how to get a Woocommerce shortcode to show all products on a page.

I’ve searched and searched, and it appears Woocommerce doesn’t have an official way to do this.

But no worries, I’m going to tell you exactly how to set this up so that you can add a filter that will allow you to use the native Woocommerce shortcode to display all products.

  1. Okay, so the first step is to open up your theme’s function file (functions.php).Open this up in a text editor or your favorite HTML editor. I use Notepad++ because it’s awesome! And yes, I do hand code most stuff.
  2. With the functions file open, go to the bottom of the file and add the following code.
    /* Add Show All Products to Woocommerce Shortcode */
    function woocommerce_shortcode_display_all_products($args)
    {
     if(strtolower(@$args['post__in'][0])=='all')
     {
      global $wpdb;
      $args['post__in'] = array();
      $products = $wpdb->get_results("SELECT ID FROM ".$wpdb->posts." WHERE `post_type`='product'",ARRAY_A);
      foreach($products as $k => $v) { $args['post__in'][] = $products[$k]['ID']; }
     }
     return $args;
    }
    add_filter('woocommerce_shortcode_products_query', 'woocommerce_shortcode_display_all_products');
  3. Save the file and re-upload to your server.

You can now use the follow shortcode to display all Woocommerce products.

[products ids="all"]

How this works is that when you call the official Woocommerce shortcode for displaying products ([products ids=""]) that script is triggered using WordPress’s filter & hooks features.

When the filter script is triggered, it queries the database for all your merchandise and compiles a list of their product IDs.

Then the filter the complied goods’ IDs to the attribute option of the Woocommerce products shortcode.

Since that shortcode allows you to list all stock by ID or SKU (and our filter script adds all IDs to the code’s options) you can now use the shortcode to list all products.

The shortcode will still function as it works by default. Only when you add the all to the ID options for the code will it then show all products.

Note that order by feature still works. For example: [products id="all" orderby="menu_order"]

If you need help or just want to thank me, submit a comment below.

2 Comments

  1. Terry Lamar

    Hi…just found this and thought the search is over!

    But, it doesnt seem to work in the latest woocommerce….any help is appreciated!

    Terry

    Reply
  2. Gallagher Website Design

    Hi Terry, thanks for leaving a comment and emailing us about this. I’m looking into the problem. I sent you an email to discuss the issue further and get it resolved. Once we do, I’ll update this post here with the fix for the latest version.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Request A Free Quote

GET OUR PRICING GUIDE

Enter your email address below, and we'll send you our current pricing guide immediately.