Change sort order from ascending to descending on only two or three categories
-
We want to change the default sort order on only a few selected WooCommerce categories from alphabetical by title, ascending (A to Z), to alphabetical by title, descending (Z to A). For the rest of the site, however, we want the order to remain ascending.
The type of page I want to sort is here: https://www.georgeglazer.com/wpmain/product-category/press-clippings-exhibitions/ As you can see, the titles begin with a year. We want to show the most recent events first, i.e. a title beginning “2020” would be first, followed by titles beginning “2019,” “2018,” etc. In the near future, this page will be broken into subcategories, so it would be good to know how to assign two or more categories to whatever code will accomplish this.
I already have the following custom code in my Child Theme to make default sorting of products in all categories alphabetical, ascending order. I assume something gets added to this code, which works fine:
* * * * *
// DEFAULT PRODUCT SORTING ALPHABETICALLY //
add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_woocommerce_get_catalog_ordering_args’ );function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET[‘orderby’] ) ? woocommerce_clean( $_GET[‘orderby’] ) : apply_filters( ‘woocommerce_default_catalog_orderby’, get_option( ‘woocommerce_default_catalog_orderby’ ) );if ( ‘alphabetical’ == $orderby_value || ‘menu_order’ == $orderby_value ) {
$args[‘orderby’] = ‘title’;
$args[‘order’] = ‘ASC’;
}return $args;
}add_filter( ‘woocommerce_default_catalog_orderby_options’, ‘custom_woocommerce_catalog_orderby’ );
add_filter( ‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’ );function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby[‘alphabetical’] = __( ‘Sort by A to Z’ );
return $sortby;
}* * * * *
In doing the research on this subject I found this article about ascending vs. descending vs. random order:
https://docs.woocommerce.com/document/custom-sorting-options-ascdesc//**
* Add custom sorting options (asc/desc) */add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘custom_woocommerce_get_catalog_ordering_args’ );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET[‘orderby’] ) ? wc_clean( $_GET[‘orderby’] ) : apply_filters( ‘woocommerce_default_catalog_orderby’, get_option( ‘woocommerce_default_catalog_orderby’ ) );
if ( ‘random_list’ == $orderby_value ) {
$args[‘orderby’] = ‘rand’;
$args[‘order’] = ”;
$args[‘meta_key’] = ”;
}
return $args;
}
add_filter( ‘woocommerce_default_catalog_orderby_options’, ‘custom_woocommerce_catalog_orderby’ );
add_filter( ‘woocommerce_catalog_orderby’, ‘custom_woocommerce_catalog_orderby’ );
function custom_woocommerce_catalog_orderby( $sortby ) { $sortby[‘random_list’] = ‘Random’;
return $sortby;
}* * * * *
And I found this article about changing the default sorting for a specific category, but it’s not exactly what I want to do because it doesn’t address the order, just the type of sorting (by date, by popularity, etc.). Also note that sorting by date means the date that the product was added to the site, not the date in the title:
https://webhostingbuddy.com/blog/woocommerce-default-sort-method-for-specific-category/
//custom function to override default sort by category
function custom_default_catalog_orderby() {
//choose categories where default sorting will be changed
if (is_product_category( array( ‘category1’, ‘category2’, ‘category3’ ))) {
return ‘date’; // sort by latest
}else{
return ‘popularity’; // sort by popularity as the default
} // end if statement
} //end function
add_filter( ‘woocommerce_default_catalog_orderby’, ‘custom_default_catalog_orderby’ ); //add the filter* * * * *
I should mention that I don’t really understand PHP, although I can use common sense to see what the patterns are. In other words, give me a recipe and I can probably follow it.
–Helen
- This topic was modified 3 years, 10 months ago by .
- This topic was modified 3 years, 10 months ago by .
The page I need help with: [log in to see the link]
- The topic ‘Change sort order from ascending to descending on only two or three categories’ is closed to new replies.