SearchWP

This Documentation is for SearchWP Version 3

Available since: 2.2

searchwp_query_orderby

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:

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

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

Parameters

Parameter Type Description
$sql String

The SearchWP optimized ORDER BY

$engine String

The engine being used (added in SearchWP 2.2)

[wpforms id="3080"]