[wp-trac] [WordPress Trac] #39242: Add caching to count_user_posts()

WordPress Trac noreply at wordpress.org
Tue Jan 17 18:46:29 UTC 2023


#39242: Add caching to count_user_posts()
-------------------------------------------------+-------------------------
 Reporter:  johnjamesjacoby                      |       Owner:  whyisjake
     Type:  enhancement                          |      Status:  accepted
 Priority:  normal                               |   Milestone:  Future
                                                 |  Release
Component:  Users                                |     Version:  3.0
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch dev-feedback needs-unit-   |     Focuses:  rest-api,
  tests needs-testing                            |  performance
-------------------------------------------------+-------------------------

Comment (by wpgurudev):

 I have refactored the `count_user_posts` function to two smaller functions

 1. **count_user_posts_for_single_type**: checks count of posts for a
 single post type. This function leverages the wp cache.

 2. **count_user_posts_for_multiple_types** to check count of posts for
 multiple post types. This iterates over each post type and calls
 `count_user_posts_for_single_type` function in turn.

 In addition to this, I have also added `clear_count_user_posts_cache`
 function which clears the cache. This function can be called on several
 conditions like author change of posts, visibility change etc.

 I have added one such instance where author change will clear the cache.
 Some other instances can include

 1. Post visibility change from public to private or vice-versa.
 2. User deletion.
 3. User role change.

 and so on..

 We can keep adding functions and hook them to appropriate actions to clear
 the cache using `clear_count_user_posts_cache` function.

 As of now, I'm clearing both the public and non-public posts count cache
 to keep things simplified.

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


More information about the wp-trac mailing list