0

I am need to get this function to get user post count where we get published posts within 24 hours, if none in 24 hours then within the week.

Ex. David Green - 10 Posts within 24 hours

Vivian Waters - 5 Posts This Week

Michael Gibbs - 8 Posts This Week

Jordan Gunn - 12 Posts within 24 hours

Here is the function by WordPress that gets ALL posts but I need posts only within 24 hours.

function count_user_posts( $userid, $post_type = 'post', $public_only = false ) {
    global $wpdb;

    $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );

    $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

    /**
     * Filters the number of posts a user has written.
     *
     * @since 2.7.0
     * @since 4.1.0 Added `$post_type` argument.
     * @since 4.3.1 Added `$public_only` argument.
     *
     * @param int          $count       The user's post count.
     * @param int          $userid      User ID.
     * @param string|array $post_type   Single post type or array of post types to count the number of posts for.
     * @param bool         $public_only Whether to limit counted posts to public posts.
     */
    return apply_filters( 'get_usernumposts', $count, $userid, $post_type, $public_only );
}
1

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.
  • 1
    You mean extending the count_user_posts() to support a date query? I think I would just use approach #1 listed here above instead and get the count from $query->found_posts; – birgire Aug 3 '17 at 13:22
  • OK can you just in the user post count function by WordPress edit the $count variable where we get the time and if not today fetch by the week. Do you have a code example of your method I'm not a backend developer just knowledgeable – jkcoding Aug 3 '17 at 13:25
  • I need this to filter the main query loop in the index.php so if an author is posting too much within 24 hours I can remove their posts in the loop. – jkcoding Aug 3 '17 at 13:28
  • Please edit the question with all relevant info, to avoid guesswork. Not sure what you mean, but you can try e.g. $args = [ 'author' => $some_user_id, 'posts_per_page' => 1, 'fields' => 'ids', 'suppress_filters' => true, 'date_query' => [['after' => '24 hour ago', 'inclusive' => true]]]; where you adjust $some_user_id. Then $query = new WP_Query( $args ); and $count_per_author = $query->found_posts; – birgire Aug 3 '17 at 14:28
  • Not sure what you mean by filtering here, but just try it out first with some given user id, – birgire Aug 3 '17 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.