Created
September 27, 2016 20:36
-
-
Save jchristopher/54823beffff5ea88c5fc047fde40f5ee to your computer and use it in GitHub Desktop.
SearchWP sort results by last word in title
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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment