[wp-trac] [WordPress Trac] #47640: WP_Posts_List_Table::get_views() doesn't allow the "Mine" counts to be filtered

WordPress Trac noreply at wordpress.org
Sat May 8 18:51:27 UTC 2021


#47640: WP_Posts_List_Table::get_views() doesn't allow the "Mine" counts to be
filtered
---------------------------------------+------------------------------
 Reporter:  pbiron                     |       Owner:  (none)
     Type:  defect (bug)               |      Status:  new
 Priority:  normal                     |   Milestone:  Awaiting Review
Component:  Posts, Post Types          |     Version:
 Severity:  normal                     |  Resolution:
 Keywords:  has-screenshots has-patch  |     Focuses:  administration
---------------------------------------+------------------------------
Changes (by pbiron):

 * keywords:  has-screenshots => has-screenshots has-patch


Comment:

 [attachment:"47640.diff"] adds 2 new filters:

 * user_posts_{$post_type}_count
 * user_posts_count

 for filtering the number of posts authored by the current user.

 For the use case described above, they could be used as follows:

 {{{
 #!php
 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).

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47640#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list