Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SearchWP sort results by last word in title
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment