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

The WordPress Search Results Function - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

By default, WordPress post titles are sorted by the date they were published. This means that if you have thousands of WordPress posts that contain “tips” in the title tag, and you search for "tips" in the WordPress search box, the ones that will be shown at the top of the results are the most recent articles.

All search queries from WordPress search forms are handled by a special WordPress file called “query.php.” This can be found in the wp-includes folder in WordPress's FTP files.

In this PHP file (query.php), the most important section in the source code is this one:

///////////////////////////////////////////

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

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

// Order by

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

$q['orderby'] = "$wpdb->posts.post_date ".$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':

break;

case 'ID':

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

break;

case 'rand':

$orderby = 'RAND()';

break;

default:

$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_date ".$q['order'];

}

///////////////////////////////////////////////////////

This is where WordPress will know whether the search results will be sorted by post date or other parameters. This is the section where the results will be sorted, whether in descending or ascending order.

Specifically (in bold):

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

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

is responsible for a descending post publishing date order in the default search results; that is, the most recent post should be shown at the top of the search results list.

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

This piece of code shows that the search results will be based on “post_date.”



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: