SearchWP

This Documentation is for SearchWP Version 3

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:

<?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 );
view raw functions.php hosted with ❤ by GitHub

Parameters

Parameter Type Description
$sql String

SQL to integrate into SearchWP’s main query

[wpforms id="3080"]
[wpforms id="3080"]
[wpforms id="3080"]
[wpforms id="3080"]
[wpforms id="3080"]
[wpforms id="3080"]
[wpforms id="3080"]
[wpforms id="3080"]
[wpforms id="3080"]
[wpforms id="3080"]