Blueprint: show number of tags being used

I am not sure if this is even possible with the current functions, but to be sure I’ll ask here :wink:

I am using checkboxes in a blueprint to allow the user to select posts by tag.

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

This works fine and all is good, but now I would like to also show the number of posts that have this tags.

So instead of just a list of all tags, I would like to have it like dogs (3), cats (4) or something that shows, that there are 3 pages using the tag dog, or 4 using cats etc

Is this possible?
Or do I have to do it using the api and generate the json for it “by hand”?


That should be possible using the “long” version of the query:

Thanks for the quick reply.
I need some more pointers, I think.

If I use

                  fetch: site.index.pluck("tags", ",", true)
                  text: "{{ page.title }}"
                  value: "{{ page.slug }}"

The checkbox-text is the one of the current page, not the one of the tag
I also tried using {{ tags.title }} or {{ tag.title }}, but then there is no text shown :confused:

ah… {{ arrayItem.value.title }} works
but now the question of how to count them?
{{ arrayItem.count }} is empty and {{ arrayItem.value.count }} is the same as title

I am guessing, that the array of pluck is unique…?

Hm, I thought about something like this:

          fetch: site.index.pluck("tags", ",", true)
          text: "{{ site.getItemCount(arrayItem.value) }}"
          value: "{{ arrayItem.value }}"

Using a custom site method, but passing an argument to the getItemCount() method doesn’t seem to work.

So I guess you have to create a JSON array that returns the desired data.

ok thank you anyway!
I sort of solved it using JSON.

For future reference:

      'pattern' => 'my-api/tags.json',
      'action'  => function () {
        $tags = [];
        foreach (site()->index()->pluck('tags', ',', true) as $k => $tag) {
          $tags['Tags'][] = [ 'value' =>  $tag, 'text' => $tag . ' (' . site()->index()->filterBy('tags', $tag)->count() . ')' ];
        return json_encode($tags);

But I ran into some other problems, but made a new posts about those.
Thank you, yet again, for your help @texnixe

I tried to use the following code in a template but instead of counting how many articles are using a specific tag it is only displaying a “1” for every tag. How can I change that to show how often a tag has been used?

<?php $tagcount = count(array(page('blog')->tags()->split())); ?>
<?= $tagcount ?>

There are several issues in your code.

Let’s say you wanted to count how many articles (i.e. children of the blog page) use the tag ‘sometag’:

$countSometag = page('blog')->children()->listed()->filterBy('tags', 'sometag')->count();
1 Like

I had to add “, ‘,’” to the filterBy to make it count as I want (found here):

<?php foreach($tags as $tag): ?>
      <?php $counttag = page('blog')->children()->listed()->filterBy('tags', $tag, ',')->count(); ?>
      <a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>">
    <button type="button">
    #<?= html($tag) ?> (<?= $counttag ?>)
    <?php endforeach ?>

Thanks for support!

1 Like