DEV Community

Cover image for Natural Sort Terms in WooCommerce & WordPress
Ntinos Mavrakis
Ntinos Mavrakis

Posted on

Natural Sort Terms in WooCommerce & WordPress

If you are looking for a way to natural sort terms/attributes from products in WooCommerce, or even categories, I've come up with a way to sort everything in natural order. For example, you have attributes in your products that are a list of lets say numbers. Your list might look like this:

  1. 1
  2. 2
  3. 205
  4. 102
  5. 110

What will happen, the list will be displayed as (default sorting):

  1. 1
  2. 2
  3. 102
  4. 110
  5. 205

In order to sort the list with a natural order like this:

  1. 1
  2. 2
  3. 102
  4. 110
  5. 205

You'll simple have to convert the results of get_terms() to an array and then sort it accordingly.

<?php

$args = array(
  'order' => 'ASC',
  'orderby' => 'name',
  'hide_empty' => true,
);

$filter = get_terms( 'pa_filter', $args );

foreach ( $filter as $term ) {
    $sort_terms[$term->name] = $term;
}

uksort( $sort_terms, 'strnatcmp');

foreach ($sort_terms as $term) {
    //If you want to display an option list
    echo "<option value=" . $term->name . ">" . $term->name . "</option>";
}

Enter fullscreen mode Exit fullscreen mode

Photo by Edu Grande on Unsplash

Top comments (0)