title: WooCommerce Custom Sort Plugin
body: <p>I'm trying to set up a custom sort on my WooCommerce site, specifically I want to sort by an attribute - size - on all of my items.  I found a tutorial to help with this - <a href="http://new.galalaly.me//2013/05/woocommerce-sort-by-custom-attributes/" rel="nofollow">http://new.galalaly.me//2013/05/woocommerce-sort-by-custom-attributes/</a> - and I thought I'd followed it pretty well, but it seems like the code there might be out of date?  </p>
  
  <p>I can get the site to recognize my custom sort, but it doesn't actually sort things based on the size, it just defaults back to alphabetical order of the product name.  However, it only recognizes items that have been added or updated since the addition of the code from the tutorial (which saves the attributes to meta data so we can sort by it).  So if the items are older items then when I sort by size they don't even show up in the results.  So clearly the code is working to some extent, I just can't seem to figure out why it's not actually sorting by size. </p>
  
  <p>I've checked that order_pa_size exists in the database and has things in the correct order, and it does.  I'm sure I'm just missing something but after trying everything I can think of I'm stumped.  Any help would be greatly appreciated.  Here's my code - </p>
  
  <pre><code>/************* Add sorting by attributes **************/
   // Code from http://new.galalaly.me//2013/05/woocommerce-sort-by-custom-attributes/
  /**
   *  Defines the criteria for sorting with options defined in the method below
   */
  add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
  function custom_woocommerce_get_catalog_ordering_args( $args ) {
      global $wp_query;
          // Changed the $_SESSION to $_GET
      if (isset($_GET['orderby'])) {
          switch ($_GET['orderby']) :
              case 'pa_size' :
                  $args['order'] = 'ASC';
                  $args['meta_key'] = 'pa_size';
                  $args['orderby'] = 'order_pa_size';
              break;
          endswitch;
      }
      return $args;
  }
  
  /**
   *  Adds the sorting options to dropdown list .. The logic/criteria is in the method above
   */
  add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
  
  function custom_woocommerce_catalog_orderby( $sortby ) {
          unset($sortby['popularity']);
          unset($sortby['rating']);
          unset($sortby['price']);
          unset($sortby['price-desc']);
      $sortby['pa_size'] = 'Sort by Size - Small to Large';
      return $sortby;
  }
  
  /**
   *  Save custom attributes as post's meta data as well so that we can use in sorting and searching
   */
  add_action( 'save_post', 'save_woocommerce_attr_to_meta' );
  function save_woocommerce_attr_to_meta( $post_id ) {
          // Get the attribute_names .. For each element get the index and the name of the attribute
          // Then use the index to get the corresponding submitted value from the attribute_values array.
      if(isset($_REQUEST['attribute_names'])){
          foreach( $_REQUEST['attribute_names'] as $index =&gt; $value ) {
              update_post_meta( $post_id, $value, $_REQUEST['attribute_values'][$index] );
          }
      }
  }
  /************ End of Sorting ***************************/
  </code></pre>