Display List of Featured Products in WooCommerce

  • iftekhar
  • Dec 5, 2016
  • 0 comment
  • 2796 Views
  • Print

WooCommerce allows site administrators to feature products on their site. Widgets for sidebar can show the featured products. You can also use the WooCommerce shrotcode to display them. However, if you are custom designing your site, you may want to query them based on your requirement and then style them according to your test. Here is a simple snippet that will query all of your “Featured Product”.

<?php
function featured_products_list() {
   echo '<div id="featured-products">';
   // query featured products
   $featured = new WP_Query(array(
      'post_type' => 'product',
      'meta_key' => '_featured',
      'meta_value' => 'yes',
      'posts_per_page' => -1));
      if($featured->have_posts()) :
         echo '<ul class="widefat" cellspacing="0">';
      while ($featured->have_posts()) : $featured->the_post();
         echo '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
      endwhile;
         echo '</ul>';
      else:
         echo 'There are no Featured Products!';
      endif;
      wp_reset_query();
   echo '</div>'; }
?>

There is nothing new on this snippet. I wrote number of posts already explaining how to query posts. Perhaps you should take a look at the WordPress codex page of WP_Query to better understand what is happening here.

Now, let me clear up some of the issues here. We are querying posts based on post meta. Every time you set a product to be featured item, it gets recorded on the post meta table with post ID and “_featured” as the key and “yes” as the value of the key. By default WooCommerce installation registers a new post type (product). On our arguments, we also mentioned that “product” is the post type that we would be querying so that our blog posts (post) doesn’t get into our way. Last but not least, we have to define how many product we want to display on our list. Using “posts_per_page” argument and “-1” as the value, all we are saying is to list all the featured posts. You can define your desired number here though.

That’s pretty much all you need to know about this snippet. However, if you have question regarding this snippet, feel free to use the comment section below.

References: Featured Product Shortcode, WP_Query

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.