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.
▶️ Please watch the video above to get all the exciting details! 👆
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.
Where To Paste The PHP Code
1. Divi Assistant
If you are using our Divi Assistant plugin, simply paste the code in the PHP tab in the custom code window in the Divi Visual Builder.
2. Child Theme
If you are using a child theme, paste this code into the functions.php file. If you don't have a child theme, you can generate a child theme directly on your site or download our free child theme.
3. Code Snippet Plugins
Otherwise, install a dedicated code snippet plugin, create a new snippet, and paste this code into the PHP code editor.
If you need help understanding where to paste the code, please check out our complete guide about where to add custom PHP code snippets in Divi.
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.
Do It With Our Divi Search Helper!
Make life easier and use the Divi Search Helper instead! This innovative plugin upgrades the existing Divi Search module with all the missing features provided by our addon with new design settings, advanced search criteria options, and improved search result pages.
0 Comments