WordPress.org

Make WordPress Core

Opened 7 years ago

Last modified 4 months ago

#17025 new enhancement

wp_list_authors() is not filterable

Reported by: kevinB Owned by:
Milestone: 5.0 Priority: normal
Severity: normal Version:
Component: Query Keywords: has-patch dev-feedback needs-refresh
Focuses: Cc:

Description

The template function wp_list_authors() is not filterable. Plugins may use existing WP_Query hooks to filter some authors' posts out of the result set, making the unfiltered authors list invalid.

The corresponding patch adds query filter 'list_authors_query'

Attachments (9)

list-authors_filter_3.2.patch (874 bytes) - added by kevinB 7 years ago.
query filter for wp_list_authors()
17025.diff (929 bytes) - added by wonderboymusic 5 years ago.
17025.2.diff (1.0 KB) - added by DrewAPicture 5 years ago.
filter docs
17025.3.diff (1.6 KB) - added by DrewAPicture 5 years ago.
Alternate approach
17025.4.diff (1.7 KB) - added by DrewAPicture 5 years ago.
'ids' fallback for false $hide_empty
17025.5.diff (1.5 KB) - added by DrewAPicture 4 years ago.
refresh
17025.6.diff (1.5 KB) - added by DrewAPicture 4 years ago.
refresh
17025.7.diff (1.1 KB) - added by Mte90 2 years ago.
patch refreshed for 4.6
17025.optimizeforeach.diff (834 bytes) - added by Mte90 2 years ago.
optimized foreach moving query outside

Download all attachments as: .zip

Change History (34)

@kevinB
7 years ago

query filter for wp_list_authors()

#1 @scribu
7 years ago

Related: #17022

#2 @scribu
7 years ago

  • Summary changed from add hook for wp_list_authors() to wp_list_authors() is not filterable

@wonderboymusic
5 years ago

#3 @wonderboymusic
5 years ago

  • Milestone changed from Awaiting Review to 3.7

The filter seems reasonable.

#4 @wonderboymusic
5 years ago

  • Keywords needs-docs added

There's a new filter

@DrewAPicture
5 years ago

filter docs

#5 @DrewAPicture
5 years ago

  • Keywords commit added; needs-docs removed

17025.2.diff adds a docblock for the list_authors_query filter hook.

#6 @nacin
5 years ago

I would prefer to not add a filter that directly operates on SQL. This is not very flexible or fun.

Can we instead filter A) $authors, and/or B) $author_count?

Also: Unless I am reading it wrong, this function also appears to be very, unnecessarily heavy. By only asking for 'ids' from get_users(), none of the users are getting cached, which means each individual call to get_usermeta() is triggering a new query.

It would make more sense to run the raw query first, then fetch only those user objects (see also cache_users(); get_users() can also do this), unless $hide_empty is false, in which case you still need to fetch all objects as we're doing now.

#7 @ocean90
5 years ago

  • Keywords commit removed

#8 @wonderboymusic
5 years ago

  • Keywords needs-refresh added

see @nacin's comment - needs alternate filter approach

@DrewAPicture
5 years ago

Alternate approach

#9 @DrewAPicture
5 years ago

  • Keywords dev-feedback added; needs-refresh removed

Would something like 17025.3.diff work better?

@DrewAPicture
5 years ago

'ids' fallback for false $hide_empty

#10 @DrewAPicture
5 years ago

  • Cc xoodrew@… added

#11 @kevinB
5 years ago

This request was part of a handful which I tossed out a couple years ago with an overall goal of allowing records to be filtered out of or into the result set. Filters applied in WP_Query and elsewhere make it possible to elevate the logged user to (or demote them from) author capabilities contextually based on criteria which WP core need not be aware of nor formally support. But some of the corresponding "user assignment" UI does not permit corresponding filtering.

wp_list_authors() is low-priority to me since it's not called by core. But inevitably some plugins and themes use it in a way that doesn't mesh with per-user inclusion/exclusion I do with WP_Query filters. Then I'll have to filter every query and use strpos() and preg_match() to determine if it is the wp_list_authors() query. Now that's not fun. So basically, the significance of this ticket is in setting a precedent for whether the API fully supports external modification of content and user queries, or whether it presumes to anticipate and regulate all implementations.

In this case, caching the "all objects" results of the default query is not sufficient since it is built on query clauses that do not account for post types other than 'post' nor visibility statuses other than 'private':

"SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' )

The 'pub_priv_sql_capability' filter in function get_posts_by_author_sql() would at least allow a different post type for the current user capability check, but even that is up for deprecation according to the inline comment. And then you still have this:

$sql .= " OR post_status = 'private'";

So a filter on the results would be better than nothing, but it has the disadvantage of forcing an additional query, which makes plugins look bad ;)

Last edited 5 years ago by kevinB (previous) (diff)

#12 @alex-ye
5 years ago

  • Cc nashwan.doaqan@… added

#13 @nacin
5 years ago

  • Keywords 3.8-early added
  • Milestone changed from 3.7 to Future Release

There's a lot going on here. Moving to 3.8.

#14 @wonderboymusic
4 years ago

  • Keywords needs-refresh added; dev-feedback removed
  • Milestone changed from Future Release to 4.0

@DrewAPicture probably wants to update his patch/docs

@DrewAPicture
4 years ago

refresh

#15 @DrewAPicture
4 years ago

  • Keywords needs-refresh removed

17025.5.diff refreshes the patch.

This ticket was mentioned in IRC in #wordpress-dev by helen. View the logs.


4 years ago

@DrewAPicture
4 years ago

refresh

#17 @DrewAPicture
4 years ago

  • Keywords 4.1-early added; 3.8-early removed
  • Milestone changed from 4.0 to Future Release

Let's work on this in 4.1. 17025.6.diff refreshes the patch at r29015. Unit tests might also need an update for $hide_empty.

#18 @chriscct7
3 years ago

  • Keywords needs-refresh added; 4.1-early removed

@Mte90
2 years ago

patch refreshed for 4.6

@Mte90
2 years ago

optimized foreach moving query outside

#19 @Mte90
2 years ago

  • Keywords dev-feedback added; needs-refresh removed

Patch refreshed, updated code and splitted in two patch. 17025.7.diff contain the new filter and 17025.optimizeforeach.diff contain the optimization of the foreach of the original patch.

#20 @jorbin
22 months ago

@DrewAPicture and @boonebgorges Can you two take a look? Would love to know your thoughts.

#21 @Mte90
21 months ago

any chance for the 4.7?

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


4 months ago

#23 @SergeyBiryukov
4 months ago

  • Milestone changed from Future Release to 5.0

#24 @afercia
4 months ago

  • Keywords needs-refresh added
  • Milestone changed from 5.0 to Future Release

17025.optimizeforeach.diff needs some coding standards: spacing, empty lines should not have spaces, etc.

#25 @afercia
4 months ago

  • Milestone changed from Future Release to 5.0
Note: See TracTickets for help on using tickets.