CMS Development

gotgroove
Member | Diamond Partner
Member | Diamond Partner

Blog Listing contents "Most Popular" instead of "Most Recent"

Does anyone know/is it even possible to alter the contents of a blog listing template to have the objects ordered by "most popular" instead of the default "most recent"?


Reason: Client wants a dropdown to switch the returned list of blog posts between most recent/most popular.


Since this is going into a coded_template, something like this would be used for the list of post objects:

{% if request.query_dict.sortby == 'popular' %}
  {# contents should have posts ordered by popularity #}
  {% for content in contents %}
  ... blog post markup ...
  {% endfor %}
{% else %}
  {# default contents list of posts #}
  {% for content in contents %}
  ... blog post markup ...
  {% endfor %}
{% endif %}


Using some javascript, I'm appending a query-string parameter to the URL when the dropdown option is selected. I have the query-string persist through pagination, but my assumption is there isn't a way to override the default contents list of objects.


I know of the hubspot {% post_listing %} module but, it doesn't appear to work with pagination, nor does it offer a lot in the way of customizing the post markup compared to what can be done with the default data returned in contents.


Any advice / insights would be appreciated.

0 Upvotes
4 Replies 4
Adesignl
Top Contributor | Partner
Top Contributor | Partner

Blog Listing contents "Most Popular" instead of "Most Recent"

There is a View attr in contents but it is always 0 or null.

 

Screen Shot 2019-05-14 at 11.34.07 AM.png

0 Upvotes
AJLaPorte_diagr
Key Advisor

Blog Listing contents "Most Popular" instead of "Most Recent"

Hey @gotgroove ,

 

You can make use of this function to grab the popular posts: https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#blog-popular-posts

 

However, you will only be able to get a limit of 200 posts and you will have to create the pagination yourself (most likely with some kind of JS). Unfortunatley, HubSpot doesnt offer data in their Developer Info for attributes like pageviews on a post, if they did, you could the do it using the standard "contents" variable. 

 

I am going to tag a few other Devs who may be able to help with this as well. @Adesignl  @Jon_McLaren @nickdeckerdevs any thoughts on this?

0 Upvotes
Jon_McLaren
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

Blog Listing contents "Most Popular" instead of "Most Recent"

I know how to get above the 200 posts threshold but I don't think that's likely necessary for what you're trying to do. I'm also not sure if in doing this you could actually get the popularity score, which might defeat the purpose. Haven't specifically tried getting posts by most popular this way.

If you really need to get above 200 posts, you should know the method of doing it is very time consuming, and relies on ajaxy javascript. You'll need to feel comfortable with that kind of JS  and some advanced HubL tricks to pull it off. (takes several hours, even if you've done this many times before.)

I won't publicly post this method though as I don't think HS wants a lot of people doing this work-around. They would rather provide direct HubL functions that get you what you need to be able to do this a different way.

Messages posted by this account have been preserved for their historical usefulness. Jon has a new profile now.
0 Upvotes
sharonlicari
Community Manager
Community Manager

Blog Listing contents "Most Popular" instead of "Most Recent"

Hi @gotgroove 

 

really appreciate all the information provided, this is really helpful for the community to assist you. 

 

Here you can find some resources that can provide some guidance: https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#blog-popular-posts

 

I am going to tag some of CMS experts to assist you in case you have any question related to your question. @AJLaPorte_diagr and @Anton  would you be able to share your knowledge with @gotgroove ?

 

Thank you

Sharon


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !




0 Upvotes