Tags Filtering using Tags and Search Plugins


Plugin used:

  1. Tags Plugin
  2. Search Plugin

I’ve managed to create and capture the tags when creating a new article. However, when I try to filter the article based on Tags, it shows an error:

Expected key slug not present in find('tagged') options argument.

In my controller:

use Cake\Utility\Hash;
public function index()
	$query = $this->Articles->find('search', ['search' => $this->request->getQuery()])->contain(['Tags']);
	$articles = $this->paginate($query)->toArray();
	$categories = $this->Articles->Categories->find('list', ['limit' => 200]);
	$tags = $this->Articles->Tagged->find()->distinct(['Tags.slug', 'Tags.label'])->contain(['Tags'])->toArray();
	$tags = Hash::combine($tags, '{n}.tag.slug', '{n}.tag.label');

In my model:

    public function initialize(array $config): void


			['label' => 'title', 'unique' => true, 'mode' => 'ascii', 'field' => 'slug']

			->add('search', 'Search.Like', [ 
				'before' => true,
				'after' => true,
				'fieldMode' => 'OR',
				'comparison' => 'LIKE',
				'wildcardAny' => '*',
				'wildcardOne' => '?',
				'fields' => ['title'],
			->callback('tag', [
				'callback' => function (Query $query, array $args, $manager) {
					// Here you would have to remap $args if key isn't the expected "tag"
					$query->find('tagged', $args);

		$this->addBehavior('Tags.Tag', ['taggedCounter' => false]);

Maybe there is something that I left in the configuration that causing this error but I cannot identify it. However, can somebody give some ideas to solve this error?

Thank you :slight_smile:

$query->find('tagged', ['tag' => $args['tag']);

Should work, also note that you have double ‘tag’ filter (value and callback). AFAIK this is not supported.

Thanks for your reply @raul338

I’ve removed the double tag filter.

Then the query should be:

$query->find('tagged', ['slug' => $args['tag']]);

Problem solved :slight_smile: