-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[63728] Enhancement: Enhance the Posts list table filtering functionality by adding an aut… #10887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -486,11 +486,82 @@ protected function categories_dropdown( $post_type ) { | |||||
| 'selected' => $cat, | ||||||
| ); | ||||||
|
|
||||||
| echo '<label class="screen-reader-text" for="cat">' . get_taxonomy( 'category' )->labels->filter_by_item . '</label>'; | ||||||
| echo '<label for="cat">' . esc_html( get_taxonomy( 'category' )->labels->filter_by_item ) . '</label>'; | ||||||
|
|
||||||
| wp_dropdown_categories( $dropdown_options ); | ||||||
| } | ||||||
| } | ||||||
| /** | ||||||
| * Displays an authors drop-down for filtering on the Posts list table. | ||||||
| * | ||||||
| * @since 6.8.0 | ||||||
| * | ||||||
| * @param string $post_type Post type slug. | ||||||
| */ | ||||||
| protected function authors_dropdown( $post_type ) { | ||||||
| /** | ||||||
| * Filters whether to remove the 'Authors' drop-down from the post list table. | ||||||
| * | ||||||
| * @since 6.8.0 | ||||||
| * | ||||||
| * @param bool $disable Whether to disable the authors drop-down. Default false. | ||||||
| * @param string $post_type Post type slug. | ||||||
| */ | ||||||
| if ( apply_filters( 'disable_authors_dropdown', false, $post_type ) ) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| $post_type_object = get_post_type_object( $post_type ); | ||||||
|
|
||||||
| // Only show author filter if post type supports authors. | ||||||
| if ( ! post_type_supports( $post_type, 'author' ) ) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // Check if there are multiple authors with published posts for this post type. | ||||||
| $authors = get_users( | ||||||
| array( | ||||||
| 'who' => 'authors', | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In thinking about this, I was realizing that this is not actually the correct data to pull for this filter. The filter shouldn't be users; it should be "people who have authored posts". Posts can be authored by people who previously had privileges, but don't any more, and this query wouldn't catch those. This actually needs to source the data based on unique values in the |
||||||
| 'has_published_posts' => array( $post_type ), | ||||||
| 'fields' => array( 'ID', 'display_name' ), | ||||||
| 'orderby' => 'display_name', | ||||||
| ) | ||||||
| ); | ||||||
|
|
||||||
| // Only display the dropdown if there are multiple authors. | ||||||
| if ( count( $authors ) < 2 ) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| $selected_author = isset( $_GET['author'] ) ? (int) $_GET['author'] : 0; | ||||||
|
|
||||||
| /** | ||||||
| * Filters the authors shown in the authors drop-down. | ||||||
| * | ||||||
| * @since 6.8.0 | ||||||
| * | ||||||
| * @param WP_User[] $authors Array of WP_User objects. | ||||||
| * @param string $post_type Post type slug. | ||||||
| */ | ||||||
| $authors = apply_filters( 'posts_authors_dropdown_authors', $authors, $post_type ); | ||||||
|
Comment on lines
+538
to
+546
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need this filter?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see a good argument for this, since the author list can be a major performance problem on sites with large numbers of authors. I think it would be better to filter the query arguments, however, not the result. Filtering the result means that you potentially run two author queries, where filtering the arguments only runs one.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After thinking more, I don't think this filter is relevant;; this isn't the right data to be querying. |
||||||
|
|
||||||
| ?> | ||||||
| <label for="filter-by-author"><?php esc_html_e( 'Filter by author' ); ?></label> | ||||||
| <select name="author" id="filter-by-author"> | ||||||
| <option value="0"<?php selected( $selected_author, 0 ); ?>><?php esc_html_e( 'All authors' ); ?></option> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| <?php | ||||||
| foreach ( $authors as $author ) { | ||||||
| printf( | ||||||
| '<option value="%1$d"%2$s>%3$s</option>', | ||||||
| (int) $author->ID, | ||||||
| selected( $selected_author, $author->ID, false ), | ||||||
| esc_html( $author->display_name ) | ||||||
| ); | ||||||
| } | ||||||
| ?> | ||||||
| </select> | ||||||
| <?php | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Displays a formats drop-down for filtering items. | ||||||
|
|
@@ -533,10 +604,9 @@ protected function formats_dropdown( $post_type ) { | |||||
|
|
||||||
| $displayed_post_format = $_GET['post_format'] ?? ''; | ||||||
| ?> | ||||||
| <label for="filter-by-format" class="screen-reader-text"> | ||||||
| <label for="filter-by-format"> | ||||||
| <?php | ||||||
| /* translators: Hidden accessibility text. */ | ||||||
| _e( 'Filter by post format' ); | ||||||
| _e( 'Format' ); | ||||||
| ?> | ||||||
| </label> | ||||||
| <select name="post_format" id="filter-by-format"> | ||||||
|
|
@@ -574,6 +644,7 @@ protected function extra_tablenav( $which ) { | |||||
| $this->months_dropdown( $this->screen->post_type ); | ||||||
| $this->categories_dropdown( $this->screen->post_type ); | ||||||
| $this->formats_dropdown( $this->screen->post_type ); | ||||||
| $this->authors_dropdown( $this->screen->post_type ); | ||||||
|
|
||||||
| /** | ||||||
| * Fires before the Filter button on the Posts and Pages list tables. | ||||||
|
|
@@ -595,8 +666,11 @@ protected function extra_tablenav( $which ) { | |||||
| $output = ob_get_clean(); | ||||||
|
|
||||||
| if ( ! empty( $output ) ) { | ||||||
| echo '<fieldset class="filters-group">'; | ||||||
| echo '<legend class="screen-reader-text">' . esc_html__( 'Filters' ) . '</legend>'; | ||||||
| echo $output; | ||||||
| submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) ); | ||||||
| echo '</fieldset>'; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it actually 6.8 or 7.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1