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

Sort Search Results by Title Plug-in - 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

Sometimes it is easier not to edit the WordPress core files such as query.php, and just use WordPress plug-in features for this. This is a more convenient approach, since we only need to upload a PHP file to a /wp-content/plugins/ directory in your FTP server. Then activate the plug-in to enable the feature.

To do this, we need a PHP file. Below is the source code,

<?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';

}

}

?>

This plug-in works by adding actions (from the plug-in) to WordPress's existing search functions. This is indicated by add_action

The PHP function sort_searchresult_by_title($k) will perform the actual sorting. This will communicate with WordPress's MySQL database to pick up post titles and then sort them by “descending” order as specified by:

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

To download the plug-in, visit the following URL: http://blog.360.yahoo.com/blog-nNv5JKEydKKD9OmfLBfY?p=2



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