Multiselect options count from children page

Greetings everyone,

Got an issue that I don’t even know where to start or do :grimacing:

I have a page called “Interests” that has a structure field where I add multiple “interests” in a subject, for example “Sports”.

Then I have a “Experiences” page with children pages (Each experience).
The previous field is populating a multiselect field in the “Experiences” children pages, being used as a category system, where I can select multiple “Interests”.

On my homepage I have an “Interests section” where it’s displayed each interest “Sports”, etc…What i want do is display the number of “experiences” that have the interest “Sports”.

Something to displayed like “Sports as 10 experiences available”

But I don’t even know what to do or how to do it…any thoughts?

Thank you for your time

You can filter experiences for each interest type, for example

$noOfSportExperiences = page('experiences')->children()->listed()->filterBy('experience', 'sports', ',')->count();

change field name to your field name.

Ok, I understand, but I have multiple interests, meaning I can write them on the structure field.

This is the field being populated inside each “experience”:

    filterInterest:
                label: 
                  en: Choose your interest
                type: multiselect
                options: query
                query: 
                  fetch: site.find('interests').intList.toStructure
                  text: "{{ structureItem.intName }}"
                  value: "{{ structureItem.intName.slug }}"

Regarding the field, it has to be the multiselect field right?

Thank you

Sure, but somehow you have to count them, that was just an example for one interest.

If you want to apply multiple interests to a single entry, then you have to use either a multiselect field or a tags field or a checkboxes field.

Great, it worked for the “sports”…but how to get it working with the custom fields being added:

$numberOfInterests = page('experiences')->children()->listed()->filterBy('filterInterest', structureItem.intName, ',')->count();

I tried like this but no luck.

Why do you need the structureitem here? After all, all you need is the interests that are actually used?

How do you output your interests on the homepage? I guess you pluck all entries from the experience pages? Then while looping through this array, you can count the pages for each interest.

Yes, you are right, but how do I get the custom names for the multiselect, that’s the part i’m not getting it…

And no…i’m not using pluck :grimacing:

You can get the custom name by using findBy() on the structure field.

$item = page('interests')->nameOfStructureField()->toStructure()->filter(function($item) use($interest) {
  return $item->intName()->slug() === $interest;
})->first();
echo $item ? $item->intName() : $interest;

Where $interest is the sluggified values that is stored in the experience pages.

What then? Probably best if you post your home page code. But I’m going to have some :sleeping: now …

Thank you so much Sonja, i’ll have a look first and see if I can get there.

And afterwards i’ll add my code here, i’m so sorry for this, second website using Kirby and it’s been a ride…

And we will talk tomorrow, thank you so much for your time.

Ok, i’ve been trying to undersatand what i’m doing wrong, but i cannot get my head around it…

I’m gonna share what i have at the moment:

Yaml for the interests page (I don’t have children pages, only a structure field to add the interests ):

intList:
            label: Add your interests
            type: structure
            fields:
              intPhoto:
                width: 1/2
                type: files
                label: Photo
                max: 1
                multiple: false
                info: "{{ file.dimensions }} Pixels • {{ file.size }} Bytes"
              intName:
                width: 1/2
                type: text
                icon: title
                label: Name
              intDesc:
                type: textarea
                icon: text
                size: tiny
                buttons: false
                label: Small description

Yaml for the experience (Single) page:

filterInterest:
            label: Choose your interest
            type: multiselect
            options: query
            query: 
              fetch: site.find('interests').intList.toStructure
              text: "{{ structureItem.intName }}"
              value: "{{ structureItem.intName.slug }}"

Interests section on the homepage:

<?php $interests = page('interests')->intList()->toStructure();?>  
<?php if ($interests->isNotEmpty()) : ?>
<section class="interests pl-40 pr-40">
    <div class="container pt-120 pb-120">
        <div class="flex gap-80 justify-between items-center mb-40">
            <h2 class="fs-32 bold"><?php echo t('experiences-every-interest') ?></h2>                
            <a href="interests.html" target="_self" class="bold fs-18"><?php echo t('show-interests') ?> »</a> 
        </div>
        <div class="list grid col-3-13 gap-20 gap-y-40">
        <?php $interests->shuffle()->limit(3);
        foreach ($interests as $interest) : ?>
            <div class="tile">
                <a href="#" target="_self" class="mb-20">
                <div class="image grid items-center align-center justify-center gradient">
                    <?php if($interest->intName()->isNotEmpty()) : ?>
                    <h3 class="light bold fs-48"><?= $interest->intName() ?></h3>
                    <?php endif; ?>
                    <?php if ($intCover = $interest->intPhoto()->toFile()): ?>                        
                        <img src="<?= $intCover->crop(520, null, 'center center', 85)->url() ?>" alt="<?= $intCover->alt() ?>" title="<?php echo t('courtesy-of') ?> <?= $intCover->photographer() ?> - <?= $intCover->link() ?>">
                    <?php endif; ?>
                </div>
                </a>
                <div class="flex items-center justify-between mb-20">
                <p class="bold">
                <?php $numberOfInterests = page('experiences')->children()->listed()->filterBy('filterInterest', 'sports', ',');?>
                <?= $numberOfInterests->count() ?>
                 <?php echo t('experiences') ?></p>
                <a class="bold" href="#" target="_self"><?php echo t('show-all') ?> »</a>
                </div>
                <?php if($interest->intDesc()->isNotEmpty()) : ?>
                <div><p><?= $interest->intDesc() ?></p></div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
        </div>
    </div>
    </section>
    <?php endif; ?>

Since I don’t have interests children pages, do i need to use pluck?

Thank you for the help @texnixe , it’s been really helpful.

Any further thoughts on this? Do you think i need to rework any of the code in order to better? use tags instead?

Kind regards

I don’t see any reason to use tags.

The advantage of using pluck() on the children of the experiences field would be that you only get the interests that are really used vs. if you get them from the structure field they might not even have any pages. So depends on use case.

Thank you for the quick reply @pixelijn, I’m not really a good programmer, and never used pluck before, i’ll have look into in order to understand how it works.