Make WordPress Core

10/27/2021 06:42:13 PM (3 years ago)

Role/Capability: Add support for capability queries in WP_User_Query.

Similar to the existing role/role__in/role__not_in query arguments, this adds support for three new query arguments in WP_User_Query:

  • capability
  • capability__in
  • capability__not_in

These can be used to fetch users with (or without) a specific set of capabilities, for example to get all users
with the capability to edit a certain post type.

Under the hood, this will check all existing roles on the site and perform a LIKE query against the capabilities user meta field to find:

  • all users with a role that has this capability
  • all users with the capability being assigned directly

Note: In WordPress, not all capabilities are stored in the database. Capabilities can also be modified using filters like map_meta_cap. These new query arguments do NOT work for such capabilities.

The prime use case for capability queries is to get all "authors", i.e. users with the capability to edit a certain post type.

Until now, 'who' => 'authors' was used for this, which relies on user levels. However, user levels were deprecated a long time ago and thus never added to custom roles. This led to constant frustration due to users with custom roles missing from places like author dropdowns.

This updates any usage of 'who' => 'authors' in core to use capability queries instead.

Subsequently, 'who' => 'authors' queries are being deprecated in favor of these new query arguments.

Also adds a new capabilities parameter (mapping to capability__in in WP_User_Query) to the REST API users controller.

Also updates twentyfourteen_list_authors() in Twenty Fourteen to make use of this new functionality, adding a new twentyfourteen_list_authors_query_args filter to make it easier to override this behavior.

Props scribu, lgladdly, boonebgorges, spacedmonkey, peterwilsoncc, SergeyBiryukov, swissspidy.
Fixes #16841.

1 edited


  • trunk/src/wp-content/themes/twentyfourteen/functions.php

    r51045 r51943  
    492492     */
    493493    function twentyfourteen_list_authors() {
    494         $contributor_ids = get_users(
    495             array(
    496                 'fields'  => 'ID',
    497                 'orderby' => 'post_count',
    498                 'order'   => 'DESC',
    499                 'who'     => 'authors',
    500             )
    501         );
     494        $args = array(
     495            'fields'     => 'ID',
     496            'orderby'    => 'post_count',
     497            'order'      => 'DESC',
     498            'capability' => array( 'edit_posts' ),
     499        );
     501        /**
     502         * Filters query arguments for listing authors.
     503         *
     504         * @since 3.3
     505         *
     506         * @param array $args Query arguments.
     507         */
     508        $args = apply_filters( 'twentyfourteen_list_authors_query_args', $args );
     510        $contributor_ids = get_users( $args );
    503512        foreach ( $contributor_ids as $contributor_id ) :
Note: See TracChangeset for help on using the changeset viewer.