Post output filter post via HublDB?

How to display posts only if the category is “Category-1”?
and let’s say I need to display 1 post from ‘Category-1’, two posts from ‘Category-4’ and one post from ‘Category-5’?
Row category - ‘Category-1’, ‘Category-2’, ‘Category-3’, ‘Category-4’, ‘Category-5’, ‘Category-6’.

{% set table = hubdb_table_rows('2621973', 'limit=3&orderBy=-hs_created_at') %}

{% for row in table %}
{% set post = content_by_id(row['post_id']) %}
{{ cat }}
<!-- Category -->
 {% for value in row['category'] %}
	 <span>{{ value.name }}</span>
 {% endfor %}
<!-- End Category -->
<br>
<!-- Title -->
<span>{{ post.name }}</span>
<!-- End Title -->
<br>
<!-- Link -->
<span>{{ post.absolute_url }}</span>
<!-- End Link -->
<br>
<!-- Image -->
<span>{{ post.featured_image }}</span>
<!-- End Image -->
<br><br>
{% endfor %}

Html:

Categy-1, Category-2
Title
URL
Image URL
---------
Category-2
Title
URL
Image URL
---------
Categy-5
Title
URL
Image URL

@Anton , feel up to the challenge?

@dennisedson,

sure :grinning_face:

Hi @Agile-Developer,

to get it right:
You would like to build a template which will be automaticly populated from different blogs?!

If so you can do it with a custom module like this:

{# start configuration #}
{% set first_blog = blog_recent_posts('module.blog_one', limit=5) %} {# change number for different limitation of posts for the first blog #}
{% set second_blog = blog_recent_posts('module.blog_two', limit=3) %} {# change number for different limitation of posts for the second blog #}
...

{# start layout #}

{% for post in first_blog %}
...
{# paste your layout for the posts from the first blog here #}
...
{% endfor %}

{% for post in second_blog %}
...
{# paste your layout for the posts from the second blog here #}
...
{% endfor %}

...

I’m using two blog functions called “blog one” and “blog two” in this code to have the option to reuse it easily. If you don’t want to have the option to change/select the blogs easily simply change the

module.blog_one

and

module.blog_two
to the blog ID.

You can find the blog ID either in the settings here
settings → tools → website → blog → select the desired blog and look in the url

Anton_0-1636383620325.png

or if you put the blog function into your module and take a look at the preview. It should look like this:

in the preview:

hope this helps

best,

Anton

(small) update:

Sorry I didn’t get it right while reading.

To get it right you’ll need those things:

  • HubDB table function
  • a text/select function for the tags // I would suggest a select function
  • a text/number function for the amount of posts you’d like to show / I suggest a number function

First of all set them up, group them and enable the repeater option at the bottom of the right sidebar(you can set a min and max value if you’d like to have those). It should look like this

Settings:

  • HubDB table: you should select your default one. By doing this the module will work right away
  • Category filter: If you’re using a select option you’ll need to recreate your categories from the HubDB. The label is up to you( for UX reasons I would suggest to use the same names as in the HubDB). The value must be entered exactly(upper/lowercase) as value. Otherwise it might not work.
  • Post limit: If you’re going with a number function you can set up a min and max value for how many posts should be displayed

And now for the fun part - the layout/hubl:

First create a for-loop for your repeater

{% for single_row in module.category_wrapper %}
...
{% endfor %}

now add the HubDB function to it

{% set table = hubdb_table_rows(single_row.hubdb_table,"category="~single_row.category_filter~"&limit="~single_row.post_limit) %} {# replace "category" with the name of your category column #}

it has some filters build in which are accessing the other settings

after you’ve done it add another for-loop to iterate through the filtered table

{% for one_result in table %}
...
{% endfor %}

to get all your data from the HubDB for your layout you’ll need to get it “printed”. Add an output to this for-loop.

It should look like this:

{% for one_result in table %}
{{ one_result }}
{% endfor %}

You can write your desired layout like this

{{ one_result["3"] }} {# the number is in my case the column I want to print #}

All together the code should look like this:

{% for single_row in module.category_wrapper %} {# start loop for each repeated option #}
{% set table = hubdb_table_rows(single_row.hubdb_table,"category="~single_row.category_filter~"&limit="~single_row.post_limit) %} {# hudb_filter; replace "select" with the name of your category column #}
{% for one_result in table %}{# start loop for each HubDB result #}
{{ one_result["3"] }} {# the number is in my case the column I want to print;replace it with your layout and use the numbers according to the columns/what the output will tell you #}
{% endfor %}{# end loop for each HubDB result #}
{% endfor %}{# end loop for each repeated option #}

hope this helps

best,

Anton