Available since: 2.4
searchwp_query_select_inject
View Parameters »Using this hook you can inject your own SQL into SearchWP’s main query when retrieving search results. This can help to influence search results in a way SearchWP does not by default.
Example: If you’re using Titles for full names, but want to sort search results by last name we can use this hook like so:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// SEARCHWP - RETURN RESULTS ALPHABETICALLY | |
// =================================================== | |
function my_searchwp_query_select_inject() { | |
global $wpdb; | |
// SELECT the last name as computed by MySQL | |
return "SUBSTRING_INDEX({$wpdb->prefix}posts.post_title, ' ', -1) as lname"; | |
} | |
add_filter( 'searchwp_query_select_inject', 'my_searchwp_query_select_inject' ); | |
function my_searchwp_query_orderby( $sql, $engine ) { | |
global $wpdb; | |
return "ORDER BY lname ASC"; | |
} | |
add_filter( 'searchwp_query_orderby', 'my_searchwp_query_orderby', 10, 2 ); |
Parameters
Parameter | Type | Description |
---|---|---|
$sql |
String |
SQL to integrate into SearchWP’s main query |