Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#54921 reviewing defect (bug)

wp_dropdown_users_args Filter not working in WordPress 5.9

Reported by: martin7ba's profile martin7ba Owned by: audrasjb's profile audrasjb
Milestone: Awaiting Review Priority: normal
Severity: major Version: 5.9
Component: Users Keywords: reporter-feedback
Focuses: Cc:

Description

I have the following code that works great on WP 5.8.3, but it is not working on WP 5.9.

I had to rollback to WP 5.8.3.

It is just showing the administartor role users, but not the custom role 'company_owner'. In version 5.8.3 works great.

<?php
add_filter( 'wp_dropdown_users_args', 'display_additional_author_roles', 99, 2 );

function display_additional_author_roles( $query_args, $parsed_args ) {
                if ( 'company' !== get_post_type() ) {
                        return $query_args;
                }

                if ( isset( $parsed_args['name'] ) && 'post_author_override' === $parsed_args['name'] ) {
                        if ( isset( $query_args['who'] ) ) {
                                unset( $query_args['who'] );
                        }

                        $query_args['role__in'] = array( 'administrator', 'company_owner' );
                }

                return $query_args;
        }

Change History (6)

#1 follow-up: @audrasjb
2 years ago

Hello @martin7ba and thank you for the report,

WordPress 5.9 introduced some change in User Queries, as announced in the related dev note: https://make.wordpress.org/core/2022/01/05/new-capability-queries-in-wordpress-5-9/

Please let us know if it doesn't solve your issue.

#2 @audrasjb
2 years ago

  • Owner set to audrasjb
  • Status changed from new to reviewing

#3 in reply to: ↑ 1 @martin7ba
2 years ago

Hi,

No it didn't solve the issue.

This is the query that gets executed and the result is only admin users

SELECT wp_users.ID,wp_users.user_login,wp_users.display_name FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id )  INNER JOIN wp_usermeta AS mt1 ON ( wp_users.ID = mt1.user_id ) WHERE 1=1 AND ( 
  ( 
    ( 
      ( 
        ( 
          ( wp_usermeta.meta_key = 'wp_capabilities' AND wp_usermeta.meta_value LIKE '{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}\"edit\\_posts\"{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}' ) 
          OR 
          ( wp_usermeta.meta_key = 'wp_capabilities' AND wp_usermeta.meta_value LIKE '{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}\"administrator\"{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}' ) 
          OR 
          ( wp_usermeta.meta_key = 'wp_capabilities' AND wp_usermeta.meta_value LIKE '{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}\"editor\"{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}' ) 
          OR 
          ( wp_usermeta.meta_key = 'wp_capabilities' AND wp_usermeta.meta_value LIKE '{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}\"author\"{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}' ) 
          OR 
          ( wp_usermeta.meta_key = 'wp_capabilities' AND wp_usermeta.meta_value LIKE '{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}\"contributor\"{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}' )
        )
      )
    ) 
    AND 
    ( 
      ( 
        ( mt1.meta_key = 'wp_capabilities' AND mt1.meta_value LIKE '{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}\"administrator\"{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}' ) 
        OR 
        ( mt1.meta_key = 'wp_capabilities' AND mt1.meta_value LIKE '{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}\"company\\_owner\"{2273a74d14d71bb774cb0881c3df3cfb378ca21504c79d6393cab7aa1b629bc9}' )
      )
    )
  )
) ORDER BY display_name ASC 

Replying to audrasjb:

Hello @martin7ba and thank you for the report,

WordPress 5.9 introduced some change in User Queries, as announced in the related dev note: https://make.wordpress.org/core/2022/01/05/new-capability-queries-in-wordpress-5-9/

Please let us know if it doesn't solve your issue.

This ticket was mentioned in Slack in #core by martin7ba. View the logs.


2 years ago

#5 @martin7ba
2 years ago

@audrasjb

I did few more tests and if I set it like below it works.

<?php
add_filter( 'wp_dropdown_users_args', array( $this, 'display_additional_author_roles' ), 99, 2 );
public function display_additional_author_roles( $query_args, $parsed_args ) {
                if ( 'company' !== get_post_type() ) {
                        return $query_args;
                }

                $query_args['capability__in'] = array( 'administrator', 'company_owner' );

                unset( $query_args['capability'] );

                return $query_args;
        }

Works with role__in too as long as I unset the capability key.
The capability is set to edit_posts by default for CPT.

When registering the CPT, the Authors support (metabox) in the CPT add / edit screen is not showing when I try to add:

<?php
'capability_type'    => array( 'company', 'companies' ),
'map_meta_cap'       => true,

The other way I can set it as follows:

<?php
public function display_additional_author_roles( $query_args, $parsed_args ) {
                if ( 'company' !== get_post_type() ) {
                        return $query_args;
                }

                $query_args['capability'] = 'publish_companies';

                return $query_args;
        }

So every role that has the publish_companies capability will be listed as author.
I am pretty sure this is the right way to go for it, but kinda confusing if we use the capability__in key.

This ticket was mentioned in Slack in #forums by ipstenu. View the logs.


2 years ago

Note: See TracTickets for help on using tickets.