Improving the way search results are displayed on your website can significantly enhance user experience and make it easier for visitors to find what they need.
Displaying search results in alphabetical order is one effective way to organize content, especially when dealing with large sets of data or products.
This article will guide you through the steps to configure your website to display search results alphabetically.
Change the search results display order using a child theme
Install and activate a Child Theme for Divi
Go to WordPress Dashboard β Appearance β Theme Files Editor
Open the functions.php file and add this PHP snippet after the existing code:
function divi_alphabetical_search_results ( $query ) {
if( $query->is_search && !is_admin() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_filter( 'pre_get_posts','divi_alphabetical_search_results' );
Change the search results display order without a child theme
Install and activate the Code Snippets plugin
Create a new PHP snippet and add this PHP snippet
function divi_alphabetical_search_results ( $query ) {
if( $query->is_search && !is_admin() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_filter( 'pre_get_posts','divi_alphabetical_search_results' );
Note: Use only one of the two methods. If you don't want to or don't have a child theme, use the second method.