Limit the Tags in the Tag cloud by how often they are mentioned

tags
#1

Hey there lovely support and users.

I would like to limit my tag cloud so that tags which are just once mentioned are not getting e.g. an own visible tag in the menu.
Lets say the tag has to be mentioned at least 3 times.

How do I do that?

$tags = $page->children()->listed()->pluck('tags', ',', true)->count();

Maybe somehow like this?

#2

Something like this:

// get all tags from the children, but don't set the third param to true, because you want the duplicates
$tags = $page->children()->pluck('tags', ',');
// array_count_values returns an array with the tag as key and the number of times it occurred in the array as value
$tags = array_count_values($tags);
// sort it in reverse order
arsort($tags);
dump($tags);

Result will be something like:

Array
(
    [tag1] => 7
    [tag2] => 5
    [tag3] => 5
    [tag4] => 2
    [tag5] => 1
    [tag6] => 1
    [tags7] => 1
)
#3

Thanks. I got the array, but I am still a little bit lost here.
I tried to cut the array at the count of 3 of its value. I use $tags later in the code, so i thouth naming it $tags_raw before kicking out the tags which has a low count.

<?php
$tags_raw = $page->children()->pluck('tags', ','); 
$tags_raw = array_count_values($tags_raw);

if($tags_raw->value()->count() > 3){
 $tags=$tags_raw;
}

I get an syntax error here. I really don’t know how to use count() :grimacing:
I also tried it like that:

if(count($tags_raw) > 3){
 $tags=$tags_raw;
}

but i guess it just checks, it there are 4 Tags and not 3 or less, but not the checking the value of the tags.
Instead of the tag names, the site is now showing the numbers from the array.

#4

Ok, if you only want the ones with a count > 3:

$tags = $page->children()->pluck('tags', ',');
$tags = array_filter(array_count_values($tags), function($value) {
  return $value > 3;
});
foreach($tags as $key => $value) {
  echo $key;
}

Using array_filter we only return the array items with a value greater than 3.

#5

It works. Thanks again for all your help!
I would never came up with this solution.