# Display Blog Posts by "Most Viewed"

**URL:** <https://community.hubspot.com/t/display-blog-posts-by-most-viewed/71885>\
**Category:** CMS Development\
**Tags:** design-manager, local-development\
**Created:** [December 28, 2022, 4:15am UTC](https://community.hubspot.com/t/display-blog-posts-by-most-viewed/71885 "2022-12-28T04:15:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unicorndev](https://avatars.discourse-cdn.com/v4/letter/u/919ad9/32.png) [@unicorndev](https://community.hubspot.com/u/unicorndev)\
**Post date:** [December 28, 2022, 4:15am UTC](https://community.hubspot.com/t/display-blog-posts-by-most-viewed/71885/1 "2022-12-28T04:15:30Z")

</div>

Hello there,  
I was wondering if anyone can look over my code and let me know where I went wrong. I want to display my blog posts by Most Viewed in order. The first shown(in screenshot below) is correct and is the most viewed. Then the order gets wonky. 5 Factors is the second most viewed and should be the next one.

 ![Screen Shot 2022-12-27 at 11.10.38 PM.jpg](https://d2qpr2jzcazej7.cloudfront.net/original/2X/6/6b6b7dbe5b22e39e70968a293a8e5d5b2af0e292.jpeg)

I got the code from the Hubl functions doc, but not sure if I am implementing it properly:

```lia-code-sample
{% call render_animation(module.animation) %}
 <section class="blog-index">
 {# Blog listing #}

 {% set pop_posts = blog_popular_posts("default", 4, "popular_all_time") %}
 {% for pop_post in pop_posts %}
 {% for content in contents %} 
 {% if loop.index == '4' %}
 {# On the blog listing page, the first post will be featured above older posts #}

 <article class="blog-index__post-wrapper">
 <div class="blog-index__post">

 {% if pop_post.featured_image and group.use_featured_image_in_summary %}
 <a class="blog-index__post-image" 
 style="background-image: url('{{ pop_post.featured_image }}');" 
 href="{{ pop_post.absolute_url }}">
 </a>
 {% endif %}
 <div class="blog-index__post-content">
 <div>
 {% set featured_tag = content.topic_list | first %}
 {% if featured_tag %}
 <span class="blog-index__post-preheader">{{ featured_tag }}</span>
 {% endif %}
 {#<h3><a href="{{ content.absolute_url }}">{{ content.name }}</a></h3>#}
 <h3><a href="{{ pop_post.absolute_url }}">{{ pop_post.name }}</a></h3>
 {% if content_group.show_summary_in_listing %}
 {#<p>{{ content.meta_description | default(content.post_summary, true) | truncatehtml(150, '...', false) }}</p> #}
 {% endif %}
 </div>
 <div class="blog-index__post-meta">
 <span class="blog-index__post-author">
 {{ pop_post.blog_post_author }}
 </span>
 <span class="blog-index__post-date">
 {{ pop_post.publish_date | datetimeformat('%b %e, %Y') }}
 </span>
 </div>
 </div>
 </div>
 </article>
 {% endif %}
 {% endfor %}
 {% endfor %} 
 {# End blog listing #}

 </section>

```

Any help is greatly appreciated 🙂

---

<div class="post-metadata">

**Author:** ![felixmacaspac](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/felixmacaspac/32/33923_2.png) [@felixmacaspac](https://community.hubspot.com/u/felixmacaspac)\
**Post date:** [December 28, 2022, 7:42am UTC](https://community.hubspot.com/t/display-blog-posts-by-most-viewed/71885/2 "2022-12-28T07:42:07Z")

</div>

Hey, @unicorndev It becomes broken because you’re looping the blog posts/contents redundantly. if you’re looping the **blog\_popular\_posts** function you don’t need to loop thru the **contents.**

**Instead of doing this:**

```lia-code-sample
{% set pop_posts = blog_popular_posts("default", 4, "popular_all_time") %}
{% for pop_post in pop_posts %}
 {% for content in contents %}
 <div class="post-title">{{ pop_post.name }}</div>
 {% endfor %}
{% endfor %}

```

**do this:**

```lia-code-sample
{% set pop_posts = blog_popular_posts("default", 4, "popular_all_time") %}
{% for pop_post in pop_posts %}
 <div class="post-title">{{ pop_post.name }}</div>
{% endfor %}

```

Just a hunch - hope it works out.

---

<div class="post-metadata">

**Author:** ![unicorndev](https://avatars.discourse-cdn.com/v4/letter/u/919ad9/32.png) [@unicorndev](https://community.hubspot.com/u/unicorndev)\
**Post date:** [January 3, 2023, 5:21pm UTC](https://community.hubspot.com/t/display-blog-posts-by-most-viewed/71885/3 "2023-01-03T17:21:04Z")

</div>

It worked! Thank you 🙂
