Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created September 27, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/54823beffff5ea88c5fc047fde40f5ee to your computer and use it in GitHub Desktop.
Save jchristopher/54823beffff5ea88c5fc047fde40f5ee to your computer and use it in GitHub Desktop.
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