Skip to content

Instantly share code, notes, and snippets.

@railmedia
Last active October 18, 2023 10:38
Show Gist options
  • Select an option

  • Save railmedia/d029745eac3686949680eb76ea7c426f to your computer and use it in GitHub Desktop.

Select an option

Save railmedia/d029745eac3686949680eb76ea7c426f to your computer and use it in GitHub Desktop.
Order WooCommerce products by slug
function order_woo_products_by_slug( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
if ( is_woocommerce() || is_tax( 'product_cat' ) ) {
// It's the main query for a WooCommerce category archive or for a WooCommerce page
$query->set( 'orderby', 'name' );
$query->set( 'order', 'ASC' );
}
}
}
add_action( 'pre_get_posts', 'order_woo_products_by_slug' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment