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.
Link To This Post
1.
Click inside the codebox2.
Right-Click then Copy3.
Paste the HTML code into your webpage
codebox
CLICK TO SAVE THE PDF VERSION OF THIS POST
Hi I was wondering how you would alter the code to use custom fields like price. I tried to simply change title to price in the php file however it didnt work. Any Suggestions are greatly appreciated.
Thanks, Mike !
Hi John,
Thanks for the feedback, I have now fixed this problem . Can you please remove the old version and download the version 2 (which is working for WP2.8.2)
This is the link: Download Version 2 of the WordPress Sort search result by Title.
I would like to use this plugin but it is causing error message on my site (v.2.8.2) like this one:
Warning: Cannot modify header information – headers already sent by (output started at /home/XXXX/public_html/XXXX/blog/wp-content/plugins/sort-searchresult-by-title/Sort_SearchResult_by_Title.php:21) in /home/XXXX/public_html/XXXX/blog/wp-includes/pluggable.php on line 865
Any ideas? Thanks!
Hi Maxim, sorry but I cannot understand Russian language. Can you translate it to English please? Thanks.
Хай, ребята, я раньше был у Вас на сайте, но забыл пароль, где его можна посмотреть?