CMS Development

vperoza
Participant | Platinum Partner
Participant | Platinum Partner

Filter blog posts by two topics

I would like to pull a list of blog posts based on two blog topics. An example is easier.

 

Let's say that my blog has these topics: 
cars, boats, trains, planes, travel, for sale, restoration

 

I want to be able to pull a set of posts that have two tags, such as:

"cars" and "for sale"

"planes" and "travel"

"boats" and "restoration"

 

I found code samples to combine two data sets ( blog_recent_posts ), but haven't seen a code sample of comparing two sets of blog posts and only displaying posts that appear in both sets.

 

One possible solution would be if there is a function comparable to array_intersect_assoc in PHP.

 

Any suggestions?

0 Upvotes
3 Replies 3
Jsum
Key Advisor

Filter blog posts by two topics

@vperoza,

 

Are you wanting to do this on your blog listing or article pages or somewhere else on your site?

 

{% set topics = content.topic_list|map('name') %}

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

This is the most basic possible logic for what you are wanting. You could expand this.

0 Upvotes
vperoza
Participant | Platinum Partner
Participant | Platinum Partner

Filter blog posts by two topics

Thank you! I had intended to create a page outside of the blog, but with your code example, I was able to update the blog listing template for a few of the topic pages. Problem solved.

0 Upvotes
Jsum
Key Advisor

Filter blog posts by two topics

Happy to help.

0 Upvotes