Skip Navigation

[Resolved] Order by SKU in WordPress Archive not working.

This thread is resolved. Here is a description of the problem and solution.

Problem:

Sort the results by SKU field in the archive page of Woocommerce taxonomy "product category".

Solution:

As a workaround you can override this settings with some custom codes, for example, add below codes into your theme/functions.php:

function sorting_archives($wp_query) {
    if ($wp_query->is_main_query()&&is_tax('product_cat')) {
        $wp_query->set('orderby', 'meta_value');
        $wp_query->set('meta_key', '_sku');
        $wp_query->set('order', 'ASC');
    }
}
 
add_filter('pre_get_posts', 'sorting_archives', 999);

Relevant Documentation:

https://developer.wordpress.org/reference/hooks/pre_get_posts/

Fires after the query variable object is created, but before the actual query is run.

This support ticket is created 6 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 2.15 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by Luo Yang 6 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#694972

I am trying to:
Configure WordPress Archives to sort a list of Woocommerce Products by SKU

Link to a page where the issue can be seen:
hidden link
Once you log in the login I have provided go to "Literature" in the main nav.

I expected to see:
The Products sorted by SKU ascending

Instead, I got: seemingly random order

#695633

Hello,

Woocommerce plugin is using a hidden custom field "_sku" to store the SKU values, I suggest you try the solution in another similar thread:
https://toolset.com/forums/topic/order-by-sku-woocommerce-and-views/#post-430900

For your reference.

#696628

Yes I have already done that.
I have revealed the _sku field and I amusing it in my view. But the problem is fhat it is NOT working when I try to use that field to sort.

#712722

It is abnormal, in case it is a compatibility problem, please try this:
1) deactivate other plugins and switch to wordpress default theme, and test again
2) If the problem still persists, please provide a database dump file (ZIP file) of your website in below private detail box, I need to test and debug it in my localhost, thanks

#720066
#723455
woocommerce-ordering.JPG

Thanks for the details, her are what I found:

1) since it is archive page of Woocommerce taxonomy "product category", the order of result is controlled by Woocommerce, in my localhost with fresh wordpress installation, there is a order menu in the archive page see screenshot woocommerce-ordering.JPG, the order settings of Views will be overridden by this setting.

2) As a workaround you can override this settings with some custom codes, for example, add below codes into your theme/functions.php:

function sorting_archives($wp_query) {
	if ($wp_query->is_main_query()&&is_tax('product_cat', 'literature')) {
		$wp_query->set('orderby', 'meta_value');
		$wp_query->set('meta_key', '_sku');
		$wp_query->set('order', 'ASC');
	}
}

add_filter('pre_get_posts', 'sorting_archives', 999);

More help:
https://developer.wordpress.org/reference/hooks/pre_get_posts/
Fires after the query variable object is created, but before the actual query is run.

#756702

This seems to have worked. Thanks.

I did have to modify this line slightly:
if ($wp_query->is_main_query()&&is_tax('product_cat', 'literature')) {

I had to remove 'literature' because I actually want this sort to be applied for all product categories and subcategories.

#764425

You are welcome