[wp-trac] [WordPress Trac] #60490: Add 'author__in' option to WP_Query 'orderby' options

WordPress Trac noreply at wordpress.org
Fri Feb 9 20:01:32 UTC 2024


#60490: Add 'author__in' option to WP_Query 'orderby' options
-----------------------------+------------------------------
 Reporter:  bburgay          |       Owner:  (none)
     Type:  feature request  |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Query            |     Version:
 Severity:  normal           |  Resolution:
 Keywords:                   |     Focuses:
-----------------------------+------------------------------

Comment (by bburgay):

 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.


 {{{
 // Used to filter values.
 $allowed_keys = array(
         'post_name',
         'post_author',
         'post_date',
         'post_title',
         'post_modified',
         'post_parent',
         'post_type',
         'name',
         'author',
         'date',
         'title',
         'modified',
         'parent',
         'type',
         'ID',
         'menu_order',
         'comment_count',
         'rand',
         'post__in',
         'post_parent__in',
         'post_name__in',
         'author__in'
 );
 }}}


 Add switch condition to switch ( $orderby )


 {{{
 case 'author__in':
     if ( ! empty( $this->query_vars['author__in'] ) ) {
         $orderby_clause = "FIELD({$wpdb->posts}.post_author," . implode(
 ',', array_map( 'absint', $this->query_vars['author__in'] ) ) . ')';
     }
     break;
 }}}


 **In get_posts function:**

 Add author_^^_in to the $force_asc array


 {{{
 // These values of orderby should ignore the 'order' parameter.
 $force_asc = array( 'post__in', 'post_name__in', 'post_parent__in',
 'author__in' );
 }}}


 I'm attaching the modified file as well. It would be awesome if someone
 can add this soon!

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60490#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list