Skip to content

Instantly share code, notes, and snippets.

@landbryo
Created June 1, 2018 18:14
Show Gist options
  • Save landbryo/bc387b477a5149936b59ab63aad882ae to your computer and use it in GitHub Desktop.
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.
/////////////////////////
// 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