Skip to main content

We received a lot of questions how to show most popular articles and posts regarding our tutorial about implementing page view counter in SilverStripe.

Here is the solution that we also implemented on our blog system.

Let's say that we have blog article page represented with class BlogPostPage and implemented logic for page hit/view counter.

- Advertisement -

How to display most popular pages

1) Create new method in the page class

* Method returns most popular blog articles
public function MostPopularBlogArticles($num = 5) { 
  return DataObject::get(
      "Status = 'Published'",
      "`PageCounter`.Counter DESC",
      "INNER JOIN PageCounter ON `PageCounter`.PageID = `BlogPostPage`.ID",

2) Add this code to the blog post page template file ( in our case)

<!-- Most Popular Blog Articles -->
  <h3>Popular Articles</h3>
    <% control MostPopularBlogArticles(5) %>
        <a href="$Link" title="$Title.XML">$MenuTitle.XML</a>
    <% end_control %>

Note: Do not forget to change class name in the code above if you want to implement most popular pages logic on your SilverStripe powered website.

- Advertisement -