Sort blog_listing by specific tag

Hi!

I’m searching for a way of showing all post of a blog organized by tag.

I need first all the post with an specific tag and then the rest of the blog posts and of coruse i need it to be paginated.

is this posible?

Hi, @JBalmaceda :waving_hand: Thanks for your question. Can you share the steps you’ve already taken with our community? Without more details about what you’ve already tried, even if it’s not working, it’s hard to offer a guess. This is because it may or may not be possible depending on what you’ve already built or are trying to customize.

Here are a few resources to help with your research:

Thanks for the additional information! —Jaycee

Here you can see it working: Historias de Clientes | Toma acción sobre tu logística de distribución

and this is my code

{% set featured_posts = blog_recent_tag_posts("default", "client-featured", 12) %}
{% set featured_post_id = featured_posts[0].id %}
{% set post_array = [] %}
{% for content in featured_posts %}
 {% do post_array.append(content.id) %}
{% endfor %}
<section class="hs-blog-post-listing hs-blog-post-listing--{{ layout }}">
 {# First loop: featured posts #}

 {% if (current_page_num == 1 && !topic) %}
 {% for content in featured_posts %}
 {# Blog listing article #}
 <article
 class="destacado card client-card hs-blog-post-listing__post hs-blog-post-listing__post--{{ layout }} {{ "hs-blog-post-listing__post--{{ columns }}" if has_columns }}"
 {% if has_full_image and content.featured_image %}
 style="background-image: url({{ content.featured_image }});"
 {% endif %}
 aria-label="{{ module.default_text.full_blog_post_summary_text }}"
 >

 {# Adds a wrapper for an overlay if full image is enabled #}

 {% if has_full_image and content.featured_image %}
 <div class="hs-blog-post-listing__post-inner-wrapper">
 {% endif %}

 {# Post image #}

 {% if not has_alternate_image or loop.index is not divisibleby 2 %}
 {{ post_image("left") }}
 {% elif has_alternate_image and loop.index is divisibleby 2 %}
 {{ post_image("right") }}
 {% endif %}

 {# Article content #}

 {{ post_content() }}

 {# Adds a wrapper for an overlay if full image is enabled #}

 {% if has_full_image and content.featured_image %}
 </div>
 {% endif %}

 </article> 
 {% endfor %}
 {% elif (current_page_num != 1 && !topic) %}
 {% for content in contents %}
 {% if content.id not in post_array %}
 <article
 class="card client-card hs-blog-post-listing__post hs-blog-post-listing__post--{{ layout }} {{ "hs-blog-post-listing__post--{{ columns }}" if has_columns }}"
 {% if has_full_image and content.featured_image %}
 style="background-image: url({{ content.featured_image }});"
 {% endif %}
 aria-label="{{ module.default_text.full_blog_post_summary_text }}"
 >

 {# Adds a wrapper for an overlay if full image is enabled #}

 {% if has_full_image and content.featured_image %}
 <div class="hs-blog-post-listing__post-inner-wrapper">
 {% endif %}

 {# Post image #}

 {% if not has_alternate_image or loop.index is not divisibleby 2 %}
 {{ post_image("left") }}
 {% elif has_alternate_image and loop.index is divisibleby 2 %}
 {{ post_image("right") }}
 {% endif %}

 {# Article content #}

 {{ post_content() }}

 {# Adds a wrapper for an overlay if full image is enabled #}

 {% if has_full_image and content.featured_image %}
 </div>
 {% endif %}

 </article>
 {% endif %}
 {% endfor %}
 {% else %}
 {% for content in contents %}
 <article
 class="card client-card hs-blog-post-listing__post hs-blog-post-listing__post--{{ layout }} {{ "hs-blog-post-listing__post--{{ columns }}" if has_columns }}"
 {% if has_full_image and content.featured_image %}
 style="background-image: url({{ content.featured_image }});"
 {% endif %}
 aria-label="{{ module.default_text.full_blog_post_summary_text }}"
 >

 {# Adds a wrapper for an overlay if full image is enabled #}

 {% if has_full_image and content.featured_image %}
 <div class="hs-blog-post-listing__post-inner-wrapper">
 {% endif %}

 {# Post image #}

 {% if not has_alternate_image or loop.index is not divisibleby 2 %}
 {{ post_image("left") }}
 {% elif has_alternate_image and loop.index is divisibleby 2 %}
 {{ post_image("right") }}
 {% endif %}

 {# Article content #}

 {{ post_content() }}

 {# Adds a wrapper for an overlay if full image is enabled #}

 {% if has_full_image and content.featured_image %}
 </div>
 {% endif %}

 </article>
 {% endfor %}
 {% endif %}
</section>

the main problem is that in pages 2,3,4…etc the numbers of post change.
for example in the second page the number of post shown is 10 insted of 12.

How can i fix this?
thanks

Hey, @JBalmaceda :waving_hand: Did you make any progress here? If not, have you already tried logging out the posts and the pages count?

One way it might look is:

{% set featured_posts = blog_recent_tag_posts("default", "client-featured", 12) %}
{% set post_count = featured_posts|length %}
<script>console.log("Page 1 - Number of featured posts: {{ post_count }}");</script>

{% for content in featured_posts %}
 <!-- Blog post logic goes here -->
{% endfor %}

{% for page_num in range(2, max_page_num) %}
 {% set current_page_posts = blog_recent_tag_posts("default", "client-featured", 12, page_num) %}
 {% set current_page_post_count = current_page_posts|length %}
 <script>console.log("Page {{ page_num }} - Number of posts: {{ current_page_post_count }}");</script>

 {% for content in current_page_posts %}
 <!-- Blog post logic goes here -->
 {% endfor %}
{% endfor %}

And use the developer console in your browser to view the logs. This should give you a visual way to track your counts.

Have fun debugging! — Jaycee