Showing most viewed posts on your WordPress site

You can use Koko Analytics to show a list of the most viewed posts (or pages, or any other post type) on your WordPress site over the last X days.

Widget

If your active WordPress theme supports widget areas, you can add the Koko Analytics Most Viewed Posts widget to any of these areas to show a list of posts.

The widget in the plugin comes with various settings that allow you to modify what posts will show and how they are displayed.

Shortcode

Alternatively, you can show the list of most viewed posts using the koko_analytics_most_viewed_posts shortcode.

Example, without any arguments (using the defaults):

[koko_analytics_most_viewed_posts]Code language: JSON / JSON with Comments (json)

Example, with explicit arguments:

[koko_analytics_most_viewed_posts number=5 days=30 post_type="post" show_date="false"]Code language: JSON / JSON with Comments (json)

The shortcode accepts the following arguments:

  • number: how many posts to show (default=5)
  • days: the number of days to look back (default=30)
  • post_type: what post types to include, accepts a comma-separated list (default=”post”)
  • show_date: whether to show the post date in the displayed list (default=false)

From your theme

If you need more control over the HTML, you can call the koko_analytics_most_viewed_posts() function from your theme or a plugin.

Example, without arguments (using the defaults):

<?php koko_analytics_most_viewed_posts(); ?>Code language: HTML, XML (xml)

Example, with explicit arguments:

<?php koko_analytics_most_viewed_posts([ 'number' => 5, 'days' => 30, 'post_type' => ['post'] ]); ?>Code language: HTML, XML (xml)

The function accepts the same set of arguments as the shortcode listed above and returns an array of WP_Post instances.

Was this article helpful? Yes / No