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

Documentation

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

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

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