Set WooCommerce Shop Page Sorting By Name
If you’d like your WooCommerce shop page to display products sorted by their name (alphabetically) by default and override other sorting options, follow these steps.
Step 1: Add the Code Snippet
You can add the following code snippet to Fluent Snippets to safely add and manage the snippet. This will set the default product sort order to “Product Name” and ensure it overrides other sorting selections.
// Step 1: Modify Default Sorting to Product Name
add_filter('woocommerce_get_catalog_ordering_args', 'custom_shop_page_default_sorting');
function custom_shop_page_default_sorting($args) {
// Set sorting to alphabetical order (product title)
$args['orderby'] = 'title';
$args['order'] = 'ASC';
return $args;
}
// Step 2: Update Default Dropdown Selection
add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');
function custom_default_catalog_orderby($default_orderby) {
// Set dropdown default sorting to "Sort by name"
return 'title';
}
// Step 3 (Optional): Force Sorting by Name on All Shop Pages
add_action('pre_get_posts', 'force_shop_sorting_by_name');
function force_shop_sorting_by_name($query) {
if (!is_admin() && $query->is_main_query() && (is_shop() || is_product_category() || is_product_tag())) {
$query->set('orderby', 'title');
$query->set('order', 'ASC');
}
}
Step 2: Where to Add the Code
- Using the Fluent Snippets Plugin
- Install the Fluent Snippets plugin from your Software Bundle
- Go to
Snippets > Add New
. - Give your snippet a title (e.g., “Shop Sorting by Name”).
- Paste the code and click Save and Activate.
Step 3: Test the Changes
- Visit your shop page.
- Products should now display alphabetically by name, overriding any other sorting selections.
Notes
- This sorting applies to the Shop, Product Category, and Product Tag pages.
- You can modify this behavior further by tweaking the conditional in the
pre_get_posts
function.