HubSpot Ideas

albertsg

Function content_by_ids to return elements in order

Hi Community,

 

The HubL function content_by_ids is returning a different order of elements than the one we pass to it.

 

For example, if we pass the following IDs:

 

[1, 2, 3, 4, 5]

 

It returns something randomized like:

 

[1, 3, 5, 2, 4]

 

So in cases where, for example, you want content editors to set a bunch of articles in a specific order, if you use content_by_ids to get all the articles information, you are forced to create nested loops to match the articles order with the order set by content editors in a module. This is not ideal since nested loops can affect performance.

 

Would be nice if Hubspot Devs could implement the same order in the object returned by content_by_ids function and until then add this information to the HubL documentation, I believe developers should know about this.

 

Thank you!

1 Reply
claudchan-alyka
Participant | Diamond Partner

Hi @albertsg ,

I having same issue.

My currnet workaround here, example:

 

```

{# Example page ids [1, 2, 3, 4, 5] #}

{% set pagesArray = [] %}
{% for page in module.select_page %}
    {% do pagesArray.append( page ) %}
{% endfor %}

 

{# Example in order as per [1, 2, 3, 4, 5] #}

{% if pagesArray %}

    <ul>

    {% for page in pagesArray %}
        {% set pageContents = content_by_ids([page]) %}
        {% for content in pageContents %}

            <li><a href="{{ content.absolute_url }}">{{ content.title }}<br> {{ content.meta_description }}<br><img src="{{ content.featured_image }}" /></a></li>
        {% endfor %}
    {% endfor %}

    </ul>

{% endif %}

```

 

Hope this will helps.