CMS Development

Nao_Mitsuhashi
Participant

How to get a list of 100+ blog ids

SOLVE

In order to display them on the blog post list page, I store the list of all the blog ids registered in hubdb in an array, and use "content_by_ids" to get the content of the corresponding ids.
However, since the number of blog posts exceeds 100 and the maximum number of posts that can be retrieved by content_by_ids is 100, I am having trouble retrieving all posts.
I'm trying to get the target id from HubDB and get the content as follows, but is there a good way to get more than 101 articles?

 

{% set dbRows = hubdb_table_rows(3410151) %}
{% set post_ids = [] %}
{% for row in dbRows %}
  {% do post_ids.append(row.post_id) %}
{% endfor %}

{% set find_contents = content_by_ids(post_ids) %}
{% set page_count = (find_contents|length / post_count_per_page)|round(0, 'floor') %}
{% if find_contents|length % post_count_per_page > 0 %}
  {% set page_count = page_count + 1 %}
{% endif %}

 

0 Upvotes
1 Accepted solution
Brownstephen101
Solution
Top Contributor | Elite Partner
Top Contributor | Elite Partner

How to get a list of 100+ blog ids

SOLVE

I would add an offset and a limit to your dbrows, call it a number of times and append them together to recreate your loop.
I should note, I've not tested this code, but in theory this should still have limit of 300 (up to 500 before you start hitting hubspot call limits if you add a couple more loops), but it will at least boost you from 100 if you have to do it this way.

For example: 

{% set dbRows = hubdb_table_rows(3410151, "&limit=100") %}
{% set dbRows2 = hubdb_table_rows(3410151, "&offset=100&limit=100") %}
{% set dbRows3 = hubdb_table_rows(3410151, "&offset=200&limit=100") %}

{% set post_ids = [] %}
{% set post_ids2 = [] %}
{% set post_ids3 = [] %}

{% for row in dbRows %}
  {% do post_ids.append(row.post_id) %}
{% endfor %}
{% for row in dbRows2 %}
  {% do post_ids2.append(row.post_id) %}
{% endfor %}
{% for row in dbRows3 %}
  {% do post_ids3.append(row.post_id) %}
{% endfor %}

{% set find_contents = content_by_ids(post_ids) + content_by_ids(post_ids2) + content_by_ids(post_ids3) %}

 

View solution in original post

3 Replies 3
Brownstephen101
Solution
Top Contributor | Elite Partner
Top Contributor | Elite Partner

How to get a list of 100+ blog ids

SOLVE

I would add an offset and a limit to your dbrows, call it a number of times and append them together to recreate your loop.
I should note, I've not tested this code, but in theory this should still have limit of 300 (up to 500 before you start hitting hubspot call limits if you add a couple more loops), but it will at least boost you from 100 if you have to do it this way.

For example: 

{% set dbRows = hubdb_table_rows(3410151, "&limit=100") %}
{% set dbRows2 = hubdb_table_rows(3410151, "&offset=100&limit=100") %}
{% set dbRows3 = hubdb_table_rows(3410151, "&offset=200&limit=100") %}

{% set post_ids = [] %}
{% set post_ids2 = [] %}
{% set post_ids3 = [] %}

{% for row in dbRows %}
  {% do post_ids.append(row.post_id) %}
{% endfor %}
{% for row in dbRows2 %}
  {% do post_ids2.append(row.post_id) %}
{% endfor %}
{% for row in dbRows3 %}
  {% do post_ids3.append(row.post_id) %}
{% endfor %}

{% set find_contents = content_by_ids(post_ids) + content_by_ids(post_ids2) + content_by_ids(post_ids3) %}

 

Nao_Mitsuhashi
Participant

How to get a list of 100+ blog ids

SOLVE

@Brownstephen101 

Thanks for the code example and for the footnote about the upper limit.
I'll try to deal with it in the way.

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

How to get a list of 100+ blog ids

SOLVE

@Indra 

Any clever methods of solving for this?

0 Upvotes