Displaying the Most Popular Entries

ExpressionEngine has channel parameters and variables available that let you count how many times an entry has been viewed.

The track_views parameter

You should only enable this on single entry pages such as a news story or product detail page, otherwise it will add to the count and distort your totals.

Here’s a channel entries example tag for our News channel:

{exp:channel:entries channel="news" limit="1" track_views="one"}
    <h3>{title}</h3>
    {body}
    <div class="date">Posted on {entry_date format="%M %d, %Y"}</div>
{/exp:channel:entries}

The track_views parameter will only accept numbers as words, i.e. one, two, three, or four. You can use these four numbered counts for different purposes but I’ve found in practice one is sufficient for most use cases.

Displaying view counts

When you’ve added the track_views parameter you can now start to show view counts in your entries using the view_count_xxx variable. The difference here is that you can show them any place the entry is mentioned, not just on single entry pages.

Here’s our channel entries tag for our News listing page:

{exp:channel:entries channel="news" limit="10"}
    <h3>{title}</h3>
    <p>This entry has been viewed {view_count_one} times.</p>
    <div class="date">Posted on {entry_date format="%M %d, %Y"}</div>
{/exp:channel:entries}

Note here we’re using {view_count_one}, this corresponds with the track_views="one" parameter we added earlier. If you’re using track_views="two" as your parameter then to show the count variable you’d use {view_count_two}.

Creating a top list

Let’s say we want to show our 10 all time most read news stories. We’ll use EE’s orderby and sort parameters to sort the entries by the view count total, and in descending order (highest number first).

<h2>Most read news</h2>
<ul>
    {exp:channel:entries channel="news" limit="10" orderby="view_count_one" sort="desc" dynamic="no"}
    <li>{title}</li>
    {/exp:channel:entries}
</ul>

Note here we’ve also added a dynamic="no" parameter so the output is constant wherever it’s used.

Limiting the top list by date

Sometimes you might want restrict the list to show entries posted, say, in the last seven days. We can do this using the same channel entries tag, but adding the start_on parameter to restrict the date range.

This example will display entries from the last rolling seven days:

<h2>Most read news</h2>
<ul>
    {exp:channel:entries channel="news" limit="10" orderby="view_count_one" sort="desc" dynamic="no" start_on="-7 days"}
    <li>{title}</li>
    {/exp:channel:entries}
</ul>

Note with this approach if you haven’t post any entries during the last seven days the list will be empty!

Refining your list further

ExpressionEngine has several other parameters you can use to control what’s gets displayed in your list, for example:

The possibilities are endless so experiment with different parameters!

Rob Allen's avatar
Rob Allen

I've been creating web sites since 1998, and working with ExpressionEngine since 2007, building and maintaining sites for individuals, charities, organisations and businesses. I love using EE to do…

Comments 1

August 31, 2023

jonny@jonnyrapp.com

Is there a way for it to show the most popular entries viewed that week by the views instead of the entry publish date?