Opened 7 months ago
Last modified 7 months ago
#60490 new feature request
Add 'author__in' option to WP_Query 'orderby' options
Reported by: | bburgay | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Query | Keywords: | |
Focuses: | Cc: |
Description
I would like to be able to preserve the author ID order given in the WP_Query author__in array.
This would work like how the orderby post__in option works but for author__in instead.
So, for example we already have:
'post__in' – Preserve post ID order given in the post__in array
And we'd be adding:
'author__in' – Preserve author ID order given in the author__in array
(https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters)
Would it be possible to add this option to WP Core?
Attachments (1)
Change History (3)
#2
@
7 months ago
I was able to solve this with the posts_orderby filter too so figured I'd add it here in case anyone else needs it for now.
function orderby_author__in($orderby, $query) { if (!empty($query->query_vars['author__in'])) { global $wpdb; $orderby = "FIELD({$wpdb->posts}.post_author," . implode(',', array_map('absint', $query->query_vars['author__in'])) . ')'; } return $orderby; } add_filter('posts_orderby', 'orderby_author__in', 10, 2);
Note: See
TracTickets for help on using
tickets.
I took a look at how WP_Query handles orderby post__in and was able to add the option for author__in with the same logic easily.
(wp-includes/class-wp-query.php)
In parse_orderby function:
Add 'author__in' to $allowed_keys.
Add switch condition to switch ( $orderby )
In get_posts function:
Add author__in to the $force_asc array
I'm attaching the modified file as well. It would be awesome if someone can add this soon!