Woocommerce how to sort products by "last modified" date?

So here is my link:

https://snake.cl/shop/?orderby=date

This shows products by date created.

The problem I have is that I update the inventory of products and I would like them to show as if "new" or "now available".

So if I could sort by last modified date, that would work I think.

I had a look at some WP parameters for queries, and tried:

https://snake.cl/shop/?orderby=modified

but that doesn't work.

Any ideas?

Thanks!

Solutions

You should try this for Sort by last modified date order by DESC.

// Apply Sort By Last Modified
add_filter( 'woocommerce_get_catalog_ordering_args', 'woo_add_postmeta_ordering_args' );
function woo_add_postmeta_ordering_args( $args_sort ) {

  $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : '';

  switch( $orderby_value ) {
     case 'last_modified':
        $args_sort['orderby']  = 'modified';
        $args_sort['order']    = 'DESC';
     break;
  }
  return $args_sort;
}

// Add "Sort By Last Modified" option in dropdown
add_filter( 'woocommerce_default_catalog_orderby_options', 'woo_add_new_postmeta_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'woo_add_new_postmeta_orderby' );

function woo_add_new_postmeta_orderby( $sortby ) {
    $sortby['last_modified'] = __( 'Sort By Last Modified', 'woocommerce' );
    return $sortby;
}

enter image description here

OK, I figured it out for anyone else that might need this.

This is the proper "query_var": ?orderby=modified-desc.

https://snake.cl/shop/?orderby=modified-desc

that sorted my products by the last modified date, i.e when I update the inventory it shows up first.

Similar questions

Display last modified date on a Wordpress site
I was wondering if anyone knows an effective way of displaying the last modified date on a Wordpress site. I'm not interested in the last modified date of a single post or page, but in a date (and maybe time) that shows the last time ANY modification was made on a site. For example: I'm aware of the the_modified_date() and the get_the_modified_date...
How to display the published date instead of last modified in this particular Wordpress code snippet
I am trying to change the way post dates are displayed in my Wordpress child theme. As of now the time shown is the last modified, and I want it to be the date I manually set in each post (i.e. original publication date). I have found a file in the parent theme that I think might contain the code culprit. I admit I don't quite understand what this ...
Get last modified date of a specific page in WordPress
The homepage of my Wordpress site needs to display the last modified date of another linked page. How can I get that dynamically?
Query database for last modified date
I am trying to query my Wordpress server to echo the date of the last modified post. In this case the database name is local and I am checking the posts table. My current code is close to what I need, it will display the date and time as Y-m-d H:i (2021-01-13 12:18) How do remove the time and display the date as d.n.Y?
Woocommerce: Sort out of stock products at the end despite sort criteria applied
I have been able to show out of stocks products at the end with the default sorting using this snippet: This works okey with the default sorting, but when you apply other sorting criteria like for example, by price, then it stops working and out of stock products become visible again from the beginning of the catalog. How to fix this snippet to alw...
Sort by custom meta value doesn't work in WooCommerce products sort by
I'm trying to add a sorting option for woocommerce products's list. The goal is to sort products by a meta value with meta key called _rating. For now I generate this meta value with a function executed at plugin's activation. Issue : Products are not sorted by meta value but by ID instead My plugin :

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.