Skip Navigation

[Resolved] Sort woocommerce products by SKU

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.


This topic contains 8 replies, has 2 voices.

Last updated by Daniel 6 years, 1 month ago.

Assigned support staff: Beda.


Hi there!

I'm trying to display woocommerce products ordered by SKU using this code someona shared in this forum:

function order_by_sku_func($query, $view_settings) {
if($view_settings['view_id']!= 129) return $query;
$query['orderby'] = 'meta_value_num';
$query['order'] = 'ASC';
$query['meta_query'][] = array(
'key' => '_sku',
'value' => 0,
'compare' => '>',
return $query;
add_filter('wpv_filter_query', 'order_by_sku_func', 10, 2);

I matched view_id = 129 to my loop view id but product list order won't change.

Any suggestions?


Thank you for contacting us here in the Support Forum

Might I ask why you do not use the Vies GUI to sort by "_sku"?
This is easy:

1. Head to Views > Settings > Hidden custom fields

2. Click on "Edit" and check "_sku" Field

3. Save this settings

4. Head to the Products view and find "_sku" Field in your "order" settings, activate it and set your preferred order (asc/desc)

This works fine just out of the box, with no PHP code needed.

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.


Hi Beda

I'm unable to find the "order" option anywere.
Where exactly is it located?


Order By.png

Find the relevant setting in my attached screenshot.

Have you successfully enabled "_sku" in the Views Settings?

Then you will find that Field now in the Order Settings of the View.

Thank you for your patience.


I see

I'm using a WordPress Archive Template to list woocommerce products. I followed this tutorial:
only WordPress Archives and Content Templates are created. Those two views doesn't have Order settings.
Is there any other way to get a custom ordered list?


WordPress Archives Views do not have a query option because by the structure of WordPress itself, we can not manipulate the query here.

We can do that only in Views (post Views)
You can build a View in Views > Add New and query Products so to sort by and change the Query as per your needs.

This View can then be used anywhere you want.

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.


Thank you Beda

I can't build a new View because I need products to be displayed in woocommerce's default shop page. I have some custom functions in use in this page, so I can't move products to another page.
I only need to sort them by SKU if it is possible. I tried some tutorials I found with no luck.
Is it possible in some way to order products by SKU in woocommerce's shop page using Toolset plugins or using a function?



It is possible to sort Custom Post Types by Custom values (products by SKU) using Views.

If you do not want to sue Views, you need a Custom Query hooking into WordPress loop.

We can not provide this as we do not change how Plugins work, but we provide a system to adapt the output and style it, as well a Render Engine (Views) to display anything where you want on the site.

I unterstand you applied your initial Code to the ARCHIVE View?

I am not sure this will work because we can not modify the WP Archive Query.
We can only style it with Views and Our Hooks and Filters-

I suggest to create a View (Post View) and use it on the Shop (or any Other) page.

You can also use a Layout to style it more and include your View in there.

If you need this specific Sort by option, this is the only way to go.

It seems -unfortunately- that for now you need custom programming work which is beyond the scope of our support (if you do can not use Views in this case)

At this point I would suggest you consider contacting one of our certified partners from this link:

You will get the custom assistance you need to get on with your project.

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.


Thank you Beda.

I see this is a bit more complicated than I thought it'd be. I'll try to create a post View and see if I can manage to fit it in the shop page without losing any custom modifications I've already made.

Thank you for you support.

The forum ‘Types Community Support’ is closed to new topics and replies.