No you can’t pass a callback function. Callback functions are PHP, so they can’t be run in MySQL. For that to work it would need to query the results from the database first, but you’d get the wrong results because they’re unsorted.
You would need to use the posts_orderby
filter to do this as part of the SQL query:
add_filter(
'posts_orderby',
function( $orderby, $query ) {
if ( ! is_admin() && $query->is_post_type_archive( 'post_type_name' ) ) {
$orderby = "SUBSTRING_INDEX(post_title, ' ', -1) ASC, post_title ASC";
}
return $orderby;
},
10,
2
);
That uses the SUBSTRING_INDEX()
MySQL function to order results by everything after the first last space in the post title, and then by the post title so that results with matching surnames are sorted by first name.
Related Posts:
- pre_get_posts with get_posts
- How-to exclude terms from the main query the most performant way?
- “pre_get_posts” firing on every query
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- How to change posts order on Category and Tag archives?
- Show Sticky Post at the top but do not show again in the loop?
- Can I set my WP_Query to be a Main Query?
- Show scheduled posts in main loop but not in WP_Query?
- WP Query – order posts by meta field first and then order the rest
- When to use WP_query(), query_posts() and pre_get_posts
- Resetting post data to previous loop in nested loops
- How to add taxonomy filter on the query fly?
- Pagination returns 404 after page 20
- Make loop display posts by alphabetical order
- get custom post type by tag
- Order Search Results Page by meta_value If no Value Return Remaining Results
- RSS feed with specific keyword
- Group posts by custom field
- Transient pagination not working properly
- get_posts() seemingly ignoring post_type
- If orderby parameter using pre_get_posts is the same for multiple posts what fallback does the query use?
- Can not switch the queried post in pre_get_posts hook
- Woocommerce custom loop to show all the products [closed]
- How to show the posts of some category first, and then all other
- Why execute the_post()?
- Pagination stops at page 6
- Paginated HTML Sitemap
- Why does the_content not work like others for a set post id?
- pre_get_posts filter meta_query without conflicting existing meta_query
- How to limit total number of posts in wp query loop?
- My Main Query Modification is Messing up my dynamic main – why?
- Pagination is broken and I need help fixing it
- How to display 7 most recent days of posts?
- How to vary post loop results layout and resume?
- Create A Loop With A Variable Number of Posts For Each Bootstrap Row?
- Get a post from other loop each n-posts in the main loop
- Loop returning only 1 result
- WordPress Loop and $post
- WP_Query ignoring tax_query when is_singular
- Excluding pages in WP_query using ACF
- reset to main loop doesnt work
- Applying posts_clauses filter to specific queries only
- Some doubts about how the main query and the custom query works in this custom theme?
- Post loop for all taxonomy terms
- Stuck in the query loop
- Only display post if published in last 24 hours?
- How to remove the most recent post from $the_query
- WP_Query loop else statement not executing
- How do I rewrite this loop as a new WP_Query style-loop?
- Include current post into loop
- How to display the featured image for each post?
- Check for the main query from the template
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- has_excerpt() not working inside wp_query
- Save (and exclude) posts from loop to use in another loop on page
- Get all posts with empty meta_value
- Loop 3 posts column wise and continue the loop into same columns
- pre_get_posts – editing query, tax_query
- Check if a post has term inside loop
- How to Order By Two Custom Fields?
- Display specific page (that is child) content on parent page
- Query Taxonomy By Page Title
- relation OR instead of AND – Filtered term ID’s in loop
- How to change the default post type over the loop?
- Query Not working as expected
- Paginate pages with dynamic query
- WP_Query loop doen’t work with my custom taxonomy
- Determine if ID is page or post and query the ID
- get_children() Archive Template
- Populate select list with meta values from all posts of a Custom Post Type
- How to show specify category template for both parent and child category
- Pagination inside the blog page not working
- Woocommerce featured products query no longer working
- Custom Query Pagination not working on static front page
- Order posts by title and custom field value?
- query all posts published by certain user id
- why doesn’t this pre_get_posts code work?
- pre_get_posts with WP_Query to prevent posts from specific tags
- modifying the loop multiple times with arguments passed through ajax to wp_query
- WP_Query causing links to not work
- Retrieving category pages from subcategory returns empty sets
- Order posts by custom column using pre_get_posts
- Iterate through ID’s in loop
- Loop through multiple custom fields with increasing number
- Different number of posts showing in development vs production server
- An archive page without post format (just standard post)
- wp_query display posts based on day
- Sorting Posts Based On Meta Value Using AJAX
- Converting an existing query_posts to WP_Query
- Pull Instagram images into an existing loop?
- Loop being strainge
- Multiple loop for “featured” items returns wrong posts
- Pagination in custom query not working [duplicate]
- Problem with WP_Query sort
- Get author meta data with no published posts in author.php?
- How to write a query-function as a query-shortcode?
- First post outside of loop, homepage only?
- Query All Posts: Either Display Most Recent or One with Particular ACF Value Chosen
- How to get rid of extra Untitled Article in html5 document outline when using new WP_Query?
- after refresh the id that shows correct in first time click, changes to 1