WordPress.org

Make WordPress Core

Opened 9 years ago

Last modified 18 months ago

#17025 new enhancement

wp_list_authors() is not filterable

Reported by: kevinB Owned by:
Milestone: Future Release 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 9 years ago.
query filter for wp_list_authors()
17025.diff (929 bytes) - added by wonderboymusic 7 years ago.
17025.2.diff (1.0 KB) - added by DrewAPicture 7 years ago.
filter docs
17025.3.diff (1.6 KB) - added by DrewAPicture 7 years ago.
Alternate approach
17025.4.diff (1.7 KB) - added by DrewAPicture 7 years ago.
'ids' fallback for false $hide_empty
17025.5.diff (1.5 KB) - added by DrewAPicture 6 years ago.
refresh
17025.6.diff (1.5 KB) - added by DrewAPicture 6 years ago.
refresh
17025.7.diff (1.1 KB) - added by Mte90 4 years ago.
patch refreshed for 4.6
17025.optimizeforeach.diff (834 bytes) - added by Mte90 4 years ago.
optimized foreach moving query outside

Download all attachments as: .zip

Change History (36)

@kevinB
9 years ago

query filter for wp_list_authors()

#1 @scribu
9 years ago

Related: #17022

#2 @scribu
9 years ago

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

@wonderboymusic
7 years ago

#3 @wonderboymusic
7 years ago

  • Milestone changed from Awaiting Review to 3.7

The filter seems reasonable.

#4 @wonderboymusic
7 years ago

  • Keywords needs-docs added

There's a new filter

@DrewAPicture
7 years ago

filter docs

#5 @DrewAPicture
7 years ago

  • Keywords commit added; needs-docs removed

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

#6 @nacin
7 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
7 years ago

  • Keywords commit removed

#8 @wonderboymusic
7 years ago

  • Keywords needs-refresh added

see @nacin's comment - needs alternate filter approach

@DrewAPicture
7 years ago

Alternate approach

#9 @DrewAPicture
7 years ago

  • Keywords dev-feedback added; needs-refresh removed

Would something like 17025.3.diff work better?

@DrewAPicture
7 years ago

'ids' fallback for false $hide_empty

#10 @DrewAPicture
7 years ago

  • Cc xoodrew@… added

#11 @kevinB
7 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 7 years ago by kevinB (previous) (diff)

#12 @alex-ye
7 years ago

  • Cc nashwan.doaqan@… added

#13 @nacin
7 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
6 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
6 years ago

refresh

#15 @DrewAPicture
6 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.


6 years ago

@DrewAPicture
6 years ago

refresh

#17 @DrewAPicture
6 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
5 years ago

  • Keywords needs-refresh added; 4.1-early removed

@Mte90
4 years ago

patch refreshed for 4.6

@Mte90
4 years ago

optimized foreach moving query outside

#19 @Mte90
4 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
4 years ago

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

#21 @Mte90
4 years ago

any chance for the 4.7?

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


2 years ago

#23 @SergeyBiryukov
2 years ago

  • Milestone changed from Future Release to 5.0

#24 @afercia
2 years 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
2 years ago

  • Milestone changed from Future Release to 5.0

#26 @johnbillion
21 months ago

  • Milestone changed from 5.0 to 5.1

#27 @pento
18 months ago

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