Note: You are viewing the documentation for Version 3View Version 4 Docs


View the installation guide, browse the Knowledge Base, find out about SearchWP's many hooks, get support

Available since: 2.2


View Parameters »

If you are attempting to customize the order of search results to a great degree, the searchwp_query_orderby filter allows you to explicitly alter the ORDER BY used in the final query.

NOTE: The main search query is extremely optimized, only SearchWP core tables and wp_posts are available. You may need to use additional filters to make available other columns you want to use which may adversely affect performance. Use with caution.

Example: To alter the main search query’s ORDER BYs, add the following to your active theme’s functions.php:

// order by Post ID descending
function my_searchwp_query_orderby( $sql, $engine ) {
global $wpdb;
return "ORDER BY {$wpdb->prefix}posts.ID DESC";
add_filter( 'searchwp_query_orderby', 'my_searchwp_query_orderby', 10, 2 );
view raw gistfile1.php hosted with ❤ by GitHub

Example: If you want to force results to be grouped by post type (regardless of overall weight) add the following to your active theme’s functions.php:

// return results in a forced order by post type, sub ordered by weight
function my_searchwp_query_orderby( $sql, $engine ) {
global $wpdb;
// we want to return all Pages first, followed by Posts
* MUST INCLUDE ___ALL___ ENABLED POST TYPES <--------------------------
return "ORDER BY FIELD({$wpdb->prefix}posts.post_type, 'page', 'post'), finalweight DESC, {$wpdb->prefix}posts.post_date DESC";
add_filter( 'searchwp_query_orderby', 'my_searchwp_query_orderby', 10, 2 );
view raw functions.php hosted with ❤ by GitHub


Parameter Type Description
$sql String

The SearchWP optimized ORDER BY

$engine String

The engine being used (added in SearchWP 2.2)

Fix keyword search on your site. No coding required!

Now you can utilize all of the content that's gone unrecognized by native WordPress keyword search instantly with SearchWP

Get SearchWP