# How to get 3 random blog posts

**URL:** <https://community.hubspot.com/t/how-to-get-3-random-blog-posts/53615>\
**Category:** CMS Development\
**Created:** [March 22, 2022, 4:42pm UTC](https://community.hubspot.com/t/how-to-get-3-random-blog-posts/53615 "2022-03-22T16:42:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnyr209](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/johnnyr209/32/35104_2.png) [@johnnyr209](https://community.hubspot.com/u/johnnyr209)\
**Post date:** [March 22, 2022, 4:42pm UTC](https://community.hubspot.com/t/how-to-get-3-random-blog-posts/53615/1 "2022-03-22T16:42:28Z")

</div>

How would I generate 3 random Blog posts? Here is what I have so far

{% set posts = ??? %}  
{% for post in posts %}  
\<div \>  
\<a href=“{{ post.absolute\_url }}”\>  
\<img data-src=“{{ resize\_image\_url(post.featured\_image, 350 ) }}” alt=“{{ post.name }}” title=“{{ post.name }}” \>  
\</a\>

\<h3\>  
\<a href=“{{ post.absolute\_url }}” class=“”\>{{ post.name }}\</a\>  
\</h3\>  
\</div\>  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![albertsg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/albertsg/32/99473_2.png) [@albertsg](https://community.hubspot.com/u/albertsg)\
**Post date:** [March 23, 2022, 5:02pm UTC](https://community.hubspot.com/t/how-to-get-3-random-blog-posts/53615/2 "2022-03-23T17:02:50Z")

</div>

Hi @johnnyr209, you could use the following functions to get blog posts:

- [HubL functions - HubSpot docs](https://developers.hubspot.com/docs/cms/hubl/functions#blog-recent-posts)

- [HubL functions - HubSpot docs](https://developers.hubspot.com/docs/cms/hubl/functions#blog-popular-posts)

And then to make it random, you could use the **random** filter ([HubL filters - HubSpot docs](https://developers.hubspot.com/docs/cms/hubl/filters#random)).

Your code could look like this:

```lia-code-sample
{% set posts = blog_recent_posts("default", 30) %}
{% for post in posts|random %}
{% if loop.index <= 3 %}
<div >
<a href="{{ post.absolute_url }}">
<img data-src="{{ resize_image_url(post.featured_image, 350 ) }}" alt="{{ post.name }}" title="{{ post.name }}" >
</a>

<h3>
<a href="{{ post.absolute_url }}" class="">{{ post.name }}</a>
</h3>
</div>
{% endif %}
{% endfor %}

```

Notice that **loop.index** will prevent the loop from displaying more than 3 articles.

In the previous example, **blog\_recent\_posts** will return 30 elements but it has a limit of 200, so play with that number as you wish (just keep in mind performance, maybe it’s no needed to get 200 articles and loop through them all to just show 3)

---

<div class="post-metadata">

**Author:** ![johnnyr209](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/johnnyr209/32/35104_2.png) [@johnnyr209](https://community.hubspot.com/u/johnnyr209)\
**Post date:** [March 24, 2022, 1:53pm UTC](https://community.hubspot.com/t/how-to-get-3-random-blog-posts/53615/3 "2022-03-24T13:53:04Z")

</div>

The code seams to be pulling a random post but it only displays 1 and not 3.

---

<div class="post-metadata">

**Author:** ![johnnyr209](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/johnnyr209/32/35104_2.png) [@johnnyr209](https://community.hubspot.com/u/johnnyr209)\
**Post date:** [March 24, 2022, 2:29pm UTC](https://community.hubspot.com/t/how-to-get-3-random-blog-posts/53615/4 "2022-03-24T14:29:38Z")

</div>

So suffle seams to work instead of random

`{% for post in posts|shuffle%}`

---

<div class="post-metadata">

**Author:** ![albertsg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/albertsg/32/99473_2.png) [@albertsg](https://community.hubspot.com/u/albertsg)\
**Post date:** [March 24, 2022, 2:51pm UTC](https://community.hubspot.com/t/how-to-get-3-random-blog-posts/53615/5 "2022-03-24T14:51:16Z")

</div>

Nice catch! I didn’t realize |random returns only 1 item 😅
