Skip Navigation

[Resolved] WooCommerce archive page ordering by custom field

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

Problem:
WooCommerce archive page ordering by custom field

Solution:
You can use the standard WordPress hook "pre_get_posts"' to modify your archive query on fly.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/woocommerce-archive-page-ordering-by-custom-field/#post-1266175

Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

This support ticket is created 5 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by jesseW-3 5 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1265913
Capture.jpg

I am trying to:
Order WooCommerce archive pages (product categories), by a custom field. Specifically I've set up a "Last Name" field so that I can order my products by "Last Name" as opposed to Product title.

Link to a page where the issue can be seen:
hidden link

I expected to see:
I was expecting so see the archive, ordered by my custom field, "Last Name" ASC

Instead, I got:
An archive which was ordered by the default output from WordPress.... aka post title ASC

PS. the duplicator file is a .daf not a .zip as I put in the ticket.

#1266175

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

It looks like the issue is due to the different slug you are using for product_cat taxonomy which is original slug.

I've added the following code to Toolset's "Custom Code" section:
=> hidden link


// change order of event_category pages to date based
function func_change_product_archive_order( $query ) {

  // Make sure this only fires when we want it too
  if( !is_admin() && $query->is_main_query() && $query->is_tax('product_cat')) {
    
    // If so, modify the order variables
    $query->set('meta_key', 'wpcf-last-name-ordering' );
    $query->set('orderby', 'meta_value'  );
    $query->set( 'order', 'ASC' );
  
  }
  
}
add_action('pre_get_posts', 'func_change_product_archive_order', 9999);

I can see now the taxonomy archive ordering is working as expected:
=> hidden link

#1266595

My issue is resolved now. Thank you for the quick and excellent support!

I'd like to know more about 'meta_value' in the following part of the code
$query->set('orderby', 'meta_value' );

I just don't understand where its coming from.... but I'm happy it works!