PHP
  Home arrow PHP arrow Page 2 - Customizing WordPress Search Results to Sort by Title
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Customizing WordPress Search Results to Sort by Title
By: Codex-M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2009-05-05


    Table of Contents:
  • Customizing WordPress Search Results to Sort by Title
  • The WordPress Search Results Function
  • Use WordPress Query.php for customized search results
  • Sort Search Results by Title Plug-in
  • Designing a Complex Application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Customizing WordPress Search Results to Sort by Title - The WordPress Search Results Function
    ( Page 2 of 5 )

    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
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek