filterBy multiple tags

Hi

quick question as I can’t seem to get it to work:
I have a blog page where the user can add tags.
Now on an overview page I would like to show all articles that have been marked via checkboxes

Checkboxes

categories:
  type: checkboxes
  options: query
  query: site.index.pluck("tags", ",", true)

Then in the template I thought I can use:
$items = $pages->children()->filterBy('tags', 'in', ['dog', 'cat']);
but it shows 0 results.

Now if I use
$items = $page->children()->filterBy('tags', 'dog', ',');
it works.

I could not find any correct usage in the docs,
for K2 I found this post, Show pages by filtering multiple tags?
which is what I tried?

Any idea?
Thanks!

I wouldnt use the checkbox field. That seems like a complication. Why not just read the tags from the pages?

see here…

Hi

thanks for your reply!

I am not sure what you mean, maybe I didn’t make myself clear:

For example:
Blog-Page:
How to treat your dog -> tags dog, etc.
Playing with your cat -> tags cat, etc.

Now I want to have an overview page, where there are all blog posts listed based on tags of the blog-posts.
So for example one page Dogs.
The user will then, based on all tags of all posts, check which posts are to be shown (= checkboxes)

So then I need to filter all blog-posts by tag

You link does not really help in so far, that I don’t want or need to show all tags that are available, but I want to filter all pages by more than one tag.
One tag is working as per the documentation

Ok, then you want to filter the children of the blog overview page.

So your second example: $items = $page->children()->filterBy('tags', 'in', ['dog', 'cat']);

is almost correct, you just have to change it to using in and the array of tags. The first with $pages cannot work, because $pages refers to all first level pages.

hang on… are you talking about a front end filter, here? are these checkboxes in the panel or in your page template?

When you say “user” do you mean someone looking at the website, or an admin in the panel?

hm.
no, even with the correct $page (my mistake) it does not work:

$filterTags = ['dog', 'cat'];
$items = $page->children()->filterBy('tags', 'in', $filterTags);

sorry.
Panel has the checkboxes
Frontend should only show the posts based on the checkboxes (which are all the tags)

Ok… i think you need to get the value of the checkbox field and split it, and pass that into the filterby()

Thanks! But I know how to do it, just that it seems that a filter by multiple values (tags) does not work

So it works with a single tag? We have the right page and are filtering the right collection?

Yes. Single tag is fine. The changing it to „in“ filter (same page, same single search tag, same collection) no results
It also does not change if I add more tags

Sorry, yes, that can’t work, because you have a comma separated list of tags stored in your content file. This will work:

$searchTags = ['trees', 'jungle'];
$items = $page->children()->filter(function($child) use($searchTags) {
  $tags = $child->tags()->split(',');
  return array_intersect($tags, $searchtags);
}));

perfect! thank you so much! that works :slight_smile:

$items = $page->children()
		->filter(function($child) use ($page) {
			return array_intersect($tags = $child->tags()->split(','), $page->categories()->split(','));
			})
		->sortBy('published', 'desc');
		
foreach ($items as $item) :
1 Like