Created
June 1, 2018 18:14
-
-
Save landbryo/bc387b477a5149936b59ab63aad882ae to your computer and use it in GitHub Desktop.
This WordPress filter and function can be used to sort posts by the second word in the title.
This file contains hidden or 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
///////////////////////// | |
// CUSTOM QUERY FILTER // | |
///////////////////////// | |
function orderby_lastname($orderby_statement) { | |
$orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC"; | |
return $orderby_statement; | |
} | |
// add filter with function before args | |
add_filter( 'posts_orderby' , 'orderby_lastname' ); | |
// add this when query is done to prevent filter from running everywhere | |
remove_filter( 'posts_orderby' , 'orderby_lastname' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment