WordPress Sort Search Result by Title Plug-in

I am beginning to wonder how to sort search results in word press by post titles. There is a plugin below.

<?php
add_action('pre_get_posts','sort_searchresult_by_title');
function sort_searchresult_by_title($k) {
if(is_search()) {
$k->query_vars['orderby'] = 'title';
$k->query_vars['order'] = 'DESC';
}
}
?>

Readme/ Instructions:

1. Download the plugin here.

2. Upload to wp-content/plugins directory in wordpress

3. Activate the plugin.

3. So everytime there is a search query made in wordpress, it will sort alphabetically in descending or ascending order.

To sort post titles by ascending order, change the last line to:

$k->query_vars['order'] = 'ASC';

If you have problems with the plugin contact the developer.



Related posts: