Add hooks to wp_count_posts()
|Reported by:||mikeschinkel||Owned by:||wonderboymusic|
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.
Change History (24)
- Keywords dev-feedback removed
- Milestone changed from Awaiting Review to 3.5
comment:10 nacin — 17 months ago
- Keywords 3.6-early added
- Milestone changed from 3.5 to Future Release
comment:18 wonderboymusic — 6 months ago
- Owner set to wonderboymusic
- Resolution set to fixed
- Status changed from new to closed