Skip to content

Instantly share code, notes, and snippets.

@zlove
Created May 22, 2015 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zlove/51c3db742aef49218083 to your computer and use it in GitHub Desktop.
Save zlove/51c3db742aef49218083 to your computer and use it in GitHub Desktop.
Order WooCommerce products by Title, ignoring a leading "The"
add_filter('posts_orderby', 'zbyte_edit_posts_orderby');
function zbyte_edit_posts_orderby($orderby_statement) {
global $wp_query;
$is_product_list =
is_shop() ||
is_product_category() ||
is_product_tag();
if ( $is_product_list && $wp_query->get('orderby') == 'title' ) {
$orderby_statement = "REPLACE(LOWER(wp_posts.post_title), 'the ', '') ASC";
}
return $orderby_statement;
}
@zlove
Copy link
Author

zlove commented May 22, 2015

Checking for the 'orderby' of 'title' should prevent this from running if the WooCommerce store admin has selected a different product ordering method from the default "Custom Ordering + name". A custom ordering will still override this. Remove the 'orderby' == 'title' check to make this override the sort on all product list pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment