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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap

Dev Shed Tutorial Topics: