-1

I'm new to twig and I'm looking to display all the articles from my craft cms by the blog tag that is selected on my index page.

I have a for each loop on my index page to display all articles, but I'm looking to only show articles with a certain tag which I have added to each article in craft cms.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Billy Brown
  • 209
  • 1
  • 5
  • 14
  • Have you tried [Array.prototype.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)? It seems to me like you just need a nudge in the right direction to what you can do with JS. I'm sure there's a similar PHP function too, if you'd prefer filtering before thee JS touches it. EDIT: There is: https://www.php.net/manual/en/function.array-filter.php – jayands Apr 15 '19 at 00:38

1 Answers1

0

Just use .relatedTo() when querying for you blog posts. It's like magic.

https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter

For example:

{% set category = entry.blogCategory.one() %} // this will be the field name of your tag

{% set posts = craft.entries()
    .section('blogPosts') // the name of your section
    .relatedTo(category) // the category object
%}
mylesthe.dev
  • 9,565
  • 4
  • 23
  • 34