In WooCommerce, search and category pages typically display results sorted by relevance or the store’s default order. This can cause older products to appear before new releases.
If you want the newest products to always appear first, just add the code below to the file functions.php from your child theme or using a snippets plugin.
1. Sort search results by date (newest first)
This code changes the order only in the product search, without affecting the general search on the website.
add_action( 'pre_get_posts', 'mudar_ordem_busca_produtos' );
function mudar_ordem_busca_produtos( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
// Garante que é a busca de produtos
if ( isset( $_GET['post_type'] ) && $_GET['post_type'] === 'product' ) {
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' ); // Mais novos primeiro
}
}
}
2. Sort product categories and tags by date (newest first)
This code changes the order on product category and tag pages.
add_action( 'pre_get_posts', 'mudar_ordem_categoria_produtos' );
function mudar_ordem_categoria_produtos( $query ) {
if ( ! is_admin() && $query->is_main_query() && ( is_product_category() || is_product_tag() ) ) {
$query->set( 'orderby', 'date' );
$query->set( 'order', 'DESC' ); // Mais novos primeiro
}
}
💡 Important notes:
- Always make a backup before editing theme files.
- If you use a theme that receives updates, add the codes to a child theme or snippet plugin so you don’t lose your changes.
- If you use an advanced search plugin (such as FacetWP or JetSmartFilters), sorting may need to be configured in it.
As a result, both the search and category and tag pages will show the newest products first, helping to highlight new releases and new items in your store.
See you next time!