Better Search Results Page
When your users type in a search query on your site, and the search results page is displayed, the results are in order by publish date of the post. In my opinion, that is not very helpful. I think it makes more sense to have them in alphabetical order instead, so in this tutorial I will show you how to show search results in alphabetical order by title.
Join subscribers on our YouTube channel and enjoy other Divi video tutorials!
Add The PHP Code Snippet
This tutorial is easy, and only requires one step to add the PHP code that works behind the scenes to adjust the output of the search results. This simply involves copying and pasting the PHP code snippet below into your website. There are a number of locations to add this, and it will totally depend on your website and experience.
If you have a child theme, then you would add this snippet to the functions.php file of a Divi child theme. If you don’t already have one, you can download our free Divi child theme here.
If you want the easy way, then I will recommend installing the Code Snippets plugin. It is a great plugin that allows you to add snippets easily in the backend of your admin area. After activating, just go to Snippets>Add New, write a title, paste the snippet, and click the save and activate button. Here is how that will look:

Snippet
// show Divi search results in alphabetical order
function pa_alphabetical_search_results( $query ) {
if( $query->is_search && !is_admin() ) {
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );
}
}
add_filter( 'pre_get_posts','pa_alphabetical_search_results' );
Optional: Reverse The Order
If for some reason you want to show the items in the opposite order, you can edit the “ASC” in the code snippet. Simply replace the ASC with DESC the items will show from Z to A.
0 Comments