User Published Post Count

Here are some different approaches:

  1. Fetch users with get_users() and paginate them with e.g. 10 per page.
    For each user, call WP_Query with date_query for the last 24 hours, else last week. Use author input argument and posts_per_page as 1 (we don’t need more) and get the total posts from WP_Query::found_posts.

    • pros: Uses only WordPress API calls
    • cons: Multiple WP_Query calls, but we limit them by the user per page count.
  2. Fetch all posts last 24 hours or last week with WP_Query and date_query. Loop through the posts and collect posts count for each post author per week or per 24 hours.

    • pros: Uses only WordPress API calls – single WP_Query call.
    • cons: Doesn’t scale well for many posts as we don’t want to fetch hundreds of posts!
  3. Write custom SQL

    • pros: Dedicated single query or queries.
    • cons: More fragile as the SQL is written by hand and we’re not using WordPress API calls. Gets complicated if we use e.g. joins and that might become slower as the tables grow.

Leave a Comment