How to filter Blog post using multiple tags


#1

Hi Developers,

How we can filter blog posts using multiple tags, now i am listing the blogs using a single TAG, but not working when we add multiple tags

{% if module.select_blog is number %}
{% set select_blog = module.select_blog %}
{% else %}
{% set select_blog = 'default' %}
{% endif %}
{% set post_tags = module.blog_tag_name|lower|replace(' ', ', ') %}

<section class="topic-post-listing padding30" style="background-color:{{ module.background_color.color }}">
<div class="page-center">
  <h2 class="title padding-bottom30">Related Posts</h2>
{% set posts = blog_recent_topic_posts( select_blog, post_tags, 3) %}
<div class="recent-listing">
{% for post in posts %}
<div class="span4 box">
  <a href="{{ post.absolute_url }}">
  <div class="image-featured">
    <span>
  <img src="{% if post.featured_image %} {{ post.featured_image }} {% else %} {{ module.default_post_image.src }} {% endif %}">
    </span>
  </div>
  </a>
  <div class="contents"> 
    <p class="post-time">{{ post.updated|datetimeformat('%B %I, %Y') }} </p>
    <h3>{{ post.name|striptags|truncate(60, breakword=False, end='...') }}</h3>
    <div class="sub-contents">{{ post.post_body|striptags|truncate(120, breakword=False, end='...')}}</div>
    <a href="{{ post.absolute_url }}" class="back-cta">Read more</a>
  </div>
</div>
{% endfor %}
</div>
<div style="clear:both;"></div>
</div>
</section>

Please anyone can help me, this works fine for me with 1 tag, but when multiple tags added, its not listing the posts. keep in i need to use it as a custom module, i am not listing this posts on blog pages (Some other pages other than blog pages)

Thanks


#3

Hi @Subin_Das,

Something like this could work to filter by two or more topics:

{% for content in contents %}
    {% if Topic1 in topics and Topic2 in topics %}
    
        {# blog listing markup #}

    {% endif %}
 {% endfor %}

For Topic1 and Topic2, you could use a couple choice or single line text fields to choose your two topics if you're in the context of a module. It'd look something like:

{% if module.choice_1 in topics and module.choice_2 in topics %}