Home arrow PHP arrow Page 3 - Customizing WordPress Search Results to Sort by Title

Use WordPress Query.php for customized search results - PHP

Sorting search results by post title in WordPress is often useful, if your website needs its entries to be sorted alphabetically. As a quick background, WordPress is the most popular open source, free blogging/CMS platform. However, the default search results are sorted by date, so there is no easy way to sort them alphabetically except to edit the core WordPress search functionally source code.

  1. Customizing WordPress Search Results to Sort by Title
  2. The WordPress Search Results Function
  3. Use WordPress Query.php for customized search results
  4. Sort Search Results by Title Plug-in
  5. Designing a Complex Application
By: Codex-M
Rating: starstarstarstarstar / 5
May 05, 2009

print this article



Now that we understand the basic functionality of WordPress search, we can  tweak this file. As with any programming task, you should back up the original query.php in your hard disk. This way, if something goes wrong, you still have the original file.

Letís have an actual example. Suppose we would like WordPress to sort by post titles in ascending order.

We will replace (in bold):

if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )

  $q['order'] = 'ASC';


$q['orderby'] = "$wpdb->posts. post_title".$q['order'];

The complete source code to be altered:


if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )

$q['order'] = 'ASC';

// Order by

if ( empty($q['orderby']) ) {

$q['orderby'] = "$wpdb->posts.post_title ".$q['order'];

} else {

// Used to filter values

$allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand');

$q['orderby'] = urldecode($q['orderby']);

$q['orderby'] = addslashes_gpc($q['orderby']);

$orderby_array = explode(' ',$q['orderby']);

if ( empty($orderby_array) )

$orderby_array[] = $q['orderby'];

$q['orderby'] = '';

for ($i = 0; $i < count($orderby_array); $i++) {

// Only allow certain values for safety

$orderby = $orderby_array[$i];

switch ($orderby) {

case 'menu_order':


case 'ID':

$orderby = "$wpdb->posts.ID";


case 'rand':

$orderby = 'RAND()';



$orderby = "$wpdb->posts.post_" . $orderby;


if ( in_array($orderby_array[$i], $allowed_keys) )

$q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;


// append ASC or DESC at the end

if ( !empty($q['orderby']))

$q['orderby'] .= " {$q['order']}";

if ( empty($q['orderby']) )

$q['orderby'] = "$wpdb->posts.post_title ".$q['order'];



Remember to back up your query.php file before finding this piece of code in the file and replacing it with the above tweaks.

>>> More PHP Articles          >>> More By Codex-M

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: