[wp-trac] [WordPress Trac] #16603: Add hooks to wp_count_posts()

WordPress Trac wp-trac at lists.automattic.com
Sun Feb 20 19:25:18 UTC 2011


#16603: Add hooks to wp_count_posts()
--------------------------+------------------------------------
 Reporter:  mikeschinkel  |      Owner:
     Type:  enhancement   |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Query         |    Version:
 Severity:  normal        |   Keywords:  has-patch dev-feedback
--------------------------+------------------------------------
 [[Image(http://mikeschinkel.com/websnaps/Posts_%E2%80%B9_Watermark_Associates_Newsletter_%E2%80%94_WordPress-20110220-142441.png)]]

 The use-case where this is needed is when the sites is using roles &
 capabilities to limit access to viewing posts to only those who have the
 proper capabilities to see them.

 For example, assume we have a system with a ''"Manager"'' role and a
 taxonomy called ''"Post Visibility"'' where terms are ''"Visible to All"''
 and  ''"Visible to Managers Only."''  We add a `'see_managers_posts'`
 capability to ''"Manager."'' We then filter outs posts with the'' "Visible
 to Managers Only"'' term using the `'posts_where'` and `'posts_join'`
 hooks when viewed by users whose role does not have the
 `'see_managers_posts'` capability.

 With that configuration now assume for example we have 10 posts with 3 of
 them ''"Visible to Managers Only."'' The post list in `/wp-admin/edit.php`
 will use `wp_count_posts()` to show that we have 10 posts when we can only
 see 7 of them. Currently to fix it so the post counts display `7` for non-
 managers we have to hook the `'query'` hook, which is a hook of last
 resort.

 So, the attached patch adds two hooks to  `wp_count_posts()`:

  - `'wp_count_posts_sql'` - To allow modifications of the SQL used to
 count posts, and
  - `'wp_count_posts'` - To allow modifications of the array returned by
 the function.

 I decided to add two (2) hooks because not having the former would mean
 we'd need to make two SQL queries if we need to modify the counts, and not
 having the latter would mean that we'd have the complexity of modifying
 SQL in use-cases where the a SQL query modification is not what is needed
 to to determine the proper counts.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/16603>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list