Opened 5 years ago
Last modified 3 years ago
#47640 new defect (bug)
WP_Posts_List_Table::get_views() doesn't allow the "Mine" counts to be filtered
Reported by: | pbiron | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | Posts, Post Types | Keywords: | has-screenshots has-patch |
Focuses: | administration | Cc: |
Description
WP_Posts_List_Table::get_views() calls wp_count_posts() to get the counts for the various post statuses.
It then computes the count for posts owned by the current user for use in the "Mine" view. That computation uses $this->user_posts_count
which is computed in WP_Posts_List_Table::__construct() with custom SQL.
The problem is that if you hook into the wp_count_posts filter and alter the counts for each post status you can end up with a situation where the "Mine" count is very different than the sum of the post status counts.
For example, I've got a CPT where some (but not all) of the posts of that type are just "placeholders" for posts of another CPT. I hide those "placeholder" posts from the list table by hooking into request and altering the request that the list table uses. I then hook into wp_count_posts
to adjust the counts by post status accordingly (e.g., don't count the "hidden" posts). This results in a situation that could be very confusing for users (see screenshot).
Thus, I suggest that a filter be added to filter the value of $this->user_posts_count
.
47640.diff adds 2 new filters:
for filtering the number of posts authored by the current user.
For the use case described above, they could be used as follows:
add_filter( 'user_posts_myposttype_count', 'myposttype_mine_count' ); function myposttype_mine_count( $post_count) { global $wpdb; $query = " SELECT COUNT( 1 ) FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON ( ID = post_id AND meta_key = %s ) WHERE post_type = %s AND post_author = %s "; $query = $wpdb->prepare( $query, 'my_meta_key', 'myposttype', get_current_user_id() ); return (int) $wpdb->get_var( $query ); }
where the presence of post meta with key
my_meta_key
tells me which posts to count (i.e., posts without it are the ones that are "hidden" from the list table).