Try this. First, add the following orderby filter in functions.php, which will allow you to customize the way your posts are sorted in the WordPress loop. By implementing this filter, you can specify different criteria for ordering your content beyond the default options, making it easier to present your posts in a way that aligns with your website’s goals. This could include sorting by custom fields, post meta data, or even custom taxonomies, providing you with greater flexibility and control over how users engage with your site. Be sure to test the changes thoroughly to ensure that they meet your needs and enhance the user experience overall.
function posts_orderby_lastname ($orderby_statement)
{
$orderby_statement = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) DESC";
return $orderby_statement;
}Code language: PHP (php)
and then use it in your query like so
add_filter( 'posts_orderby' , 'posts_orderby_lastname' );
$loop = new WP_Query(
array (
'post_type' => 'staff',
'staff-type' => $type
)
);
remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );Code language: PHP (php)
and after the loop remove the filter
