Alternative to querying post using content_by_id()

I have a link field which allows a user to select a post or page from HubSpot.

Then, I’m using the id to query that link field using content_by_id(). However, this function and content_by_ids() has a cap of 10 calls per page.

I cannot change the link field type and I’m using that field type because I need:

  • The post / page title
  • The post / page url

A single field helps achieve both as I can access the object.

But as mentioned, I need a way to get this working for pages which have more than 10 link items.

See my current approach:

{% for item in module.tag %}

 {% set href = item.url.href %}
 {% set content_id = item.url.content_id %}
 {% set get_title = content_by_id(content_id) %}

 <li class="words__li">
 <a href="{{ href }}" class="words__link" {% if item.open_in_new_tab %}target="_blank"{% endif %} {% if item.rel %}rel="{{ item.rel }}"{% endif %}>
 {{ get_title.title }}
 </a>
 </li>

{% endfor %}

Any ideas?

@stefen , any suggestions for this?

Hi @Amit_95, 10 calls per page using content_by_ids means 1000 posts/pages. Would you need more than 1000 links?
Notice that content_by_ids returns up to 100 elements while content_by_id returns only 1.

content_by_id() --> 10 calls * 1 page/call = 10 pages

content_by_ids() --> 10 calls * 100 pages/call = 1000 pages

You could store all the IDs into an array and use contents_by_ids() to get all your data in just one call :slightly_smiling_face:

Hi @albertsg ,

Thanks for clarifying, managed to get it working :slightly_smiling_face:

For anyone looking for a solution, here’s how I did it:

<!-- add items to array -->

{% set array = [] %}

{% for item in module.tag %}
 {% do array.append( item.url.content_id ) %}
{% endfor %}

{% set contents = content_by_ids(array) %}

{% for content in contents %}
	<li class="words__li">
 <a href="{{ content.absolute_url }}" class="words__link" {% if content.open_in_new_tab %}target="_blank"{% endif %} {% if content.rel %}rel="{{ content.rel }}"{% endif %}>
 {{ content.title }}
 </a>
 </li>
{% endfor %}