Filter by tags of multiple pages

I have a complex situation, I think

I have a template of a book page. I have several books there already.
Each book has a field of tags and there are multiple tags for each book.
Important: The tags are pages too!

I have another template of magazine page. In this page the user picks 5 book pages (from book pages list) that are related to this magazine page.

I would want to have a related book pages to this magazine page according to the picked books’ tags.

between book pages I use this script to get all related books that has 1 or more same tags:

$tags = page('books')->children()->not($page)->filterBy('tagss', 'in', $page->tagss()->split(','), ',')

but if I want to get al related books to a magazine page (that has tags of 4 books) I get a little confused.

I tried this, just to see if I can get a list of the tags but it gives me error:

    $book_picks = $page->picks()->toPages(',');
    $pick_tags = $book_picks->tags()->toPages(',');
    echo $pick_tags

I guess that I need to use foreach, but then I wonder how can I use it as a variable inside the filterBy.

I hope Im being understood. Thanks for the help!

  1. Please show me the field definitions of the picks field and the tags field.

  2. This cannot possibly work because tags is a field in a single page, but you try to get it from a collection.

    So you either have to loop through the pages, or you have to pluck() the values here and convert to pages.

    $pick_tags = $book_picks->pluck('tags'); // need to put separator based on how values are stored. See 1.

This is the ‘tags’ field in the ‘book’ page:

      label: Tags
      type: multiselect
         type: query

And this is the field ‘picks’ in the ‘magazine item’ page:

        label: Pick 5 book recommendations
        type: multiselect
        max: 5
          type: query


$book_picks = $page->picks()->toPages(',');
$pick_tags = $book_picks->pluck('tags', ',', true);

Does this return an array of page ids?

1 Like

it seems to work now
I used it like this:

    $book_picks = $page->recommendations()->toPages(',');
    $pick_tags = $book_picks->pluck('tagss', ',', true);
    $tags = page('books')->children()->not($page)->filterBy('tags', 'in', $pick_tags, ',');

but the dump($pick_tags) return this:
[0] => tags/photography
[1] => tags/hard-cover
[2] => tags/art
[3] => tags/paintings
makes sense?

Thanks a lot!