Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #18199
    louxu
    Participant

    Hi,

    May I know it is possible to get view count for each user role? I would like to know the total count post views for different user role instead of only exclude specific user role.

    Thank you!

    Bests,
    Diana

    #18267
    Bartosz
    Keymaster

    There’s no direct way to do it but this can be achieved using two queries.

    1. Get the user ids of a specific role by get_users() function and ‘role__in’ parameter https://codex.wordpress.org/Function_Reference/get_users
    2. Then use the user ids you got to run a WP Query https://codex.wordpress.org/Class_Reference/WP_Query – here set the ‘author__in’ parameter with the user ids (to get posts of specific user roles) and set ‘orderby’ parameter to ‘post_views’ (to get post views data along with other post data).
    3. In result PVC will extend the WP Query data with ‘total_views’ property giving you a sum of all the views of the posts found in the WP Query call.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.