# Fetching Tags Problem

**URL:** <https://forum.getkirby.com/t/fetching-tags-problem/2580>\
**Category:** Questions\
**Tags:** v2\
**Created:** [November 21, 2015, 10:05am UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580 "2015-11-21T10:05:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jannickteoh](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@jannickteoh](https://forum.getkirby.com/u/jannickteoh)\
**Post date:** [November 21, 2015, 10:05am UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/1 "2015-11-21T10:05:06Z")

</div>

Hi,  
I’ve been working on some kind of blog. I’ve built almost everything with the documentations but somehow I can’t get the tag filterBy function to work, as documented in the docs.

This is how I fetch the articles according to the blog solution. I added the counter at the beginning for a script.

```
<?php $i = 0; foreach($pages->children()->visible()->flip() as $buch): ?>

```

How to do combine this with that:

```
// fetch the basic set of articles
$articles = $page->children()->visible()->flip();

// add the tag filter
if($tag = param('tag')) {
   $articles = $articles->filterBy('tags', $tag, ',');
}

```

If tried all sorts of combinations but for know I couldn’t get it to work…

Happy to hear your solutions

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 21, 2015, 12:33pm UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/2 "2015-11-21T12:33:05Z")

</div>

Just use an if-else statement:

```php
<?php
if($tag = param('tag')) {
   $articles = $page->children()->visible()->filterBy('tags', $tag, ',')->flip();
}
else {
  $articles = $page->children()->visible()->flip();
}

```

---

<div class="post-metadata">

**Author:** ![jannickteoh](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@jannickteoh](https://forum.getkirby.com/u/jannickteoh)\
**Post date:** [November 22, 2015, 2:11am UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/4 "2015-11-22T02:11:00Z")

</div>

Thanks for the hint.

I’ve got I to work like it’s presented in the documentation. I’m working with the controller method.

I’m facing the problem that some of my »tags« are two words separated by spaces. I’ve read that there is the  
urldecode( ). I couldn’t get that to work with the following function:

```
if ($sammlung = param('sammlung')) {
$articles = $articles->filterBy('sammlung', $sammlung, ',');
};
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 22, 2015, 7:11am UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/5 "2015-11-22T07:11:34Z")

</div>

`urlencode()` the links to your tags and then `urldecode()` in the `filterBy()` function:

```php
<a href="<?php echo $page->url() . '/sammlung:' . urlencode($sammlung) ?>">

```

```php
if ($sammlung = param('sammlung')) {
$articles = $articles->filterBy('sammlung', urldecode($sammlung), ',');
};

```

---

<div class="post-metadata">

**Author:** ![jannickteoh](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@jannickteoh](https://forum.getkirby.com/u/jannickteoh)\
**Post date:** [November 22, 2015, 8:29pm UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/6 "2015-11-22T20:29:50Z")

</div>

That worked out well! Thank you very much.

But know I’m getting to the next problem:

1. One filed is input for authors. If there are listed two authors, the link is generated for both authors. Is there a way to generate two separate links for each author divide by a comma?  
A workaround would be two have two separate input fields in the backend, but is there a programmed solution?

2. Although the values are en- and decoded German umlauts are somehow not correctly interpreted, so that the generated link won’t finde any entry.

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [November 22, 2015, 9:33pm UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/7 "2015-11-22T21:33:46Z")

</div>

> [@jannickteoh](#):
>
> Is there a way to generate two separate links for each author divide by a comma?

Sure:

```php
foreach(explode(',', $page->authors()) as $author):
  // Remove whitespace around the name
  $author = trim($author);
?>
  <a href="<?php echo $page->url() . '/author:' . urlencode($author) ?>">
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 22, 2015, 10:13pm UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/8 "2015-11-22T22:13:43Z")

</div>

Or the Kirby way:

```php
foreach ($page->authors()->split() as $author) {
//do stuff
}

```

---

<div class="post-metadata">

**Author:** ![jannickteoh](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@jannickteoh](https://forum.getkirby.com/u/jannickteoh)\
**Post date:** [November 22, 2015, 11:03pm UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/9 "2015-11-22T23:03:41Z")

</div>

Hmm tried to implement it, but it failed with either of them :

```
<span class="buch-uav"><span class="discription">AUTOR</span>
    <a href="<?php echo $page->url() . '/autor:' . urlencode($article->autor()->html()) ?>">
    <?php echo $article->autor()->html() ?></a> 			
</span>

```

Could someone be so nice and help me.  
Thank you very much in advanced

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [November 23, 2015, 10:10am UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/10 "2015-11-23T10:10:41Z")

</div>

What exactly does not work? Do you still get wrong urls? Maybe you could post your complete code, i.e. controller and template?

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [November 23, 2015, 12:16pm UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/11 "2015-11-23T12:16:02Z")

</div>

By using `$article->autor()->html()`, you are again using the contents of the whole field. Try using `$author` instead.

---

<div class="post-metadata">

**Author:** ![jannickteoh](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@jannickteoh](https://forum.getkirby.com/u/jannickteoh)\
**Post date:** [November 23, 2015, 9:14pm UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/12 "2015-11-23T21:14:05Z")

</div>

Something wrong, I tried several possibilities yesterday but I didn’t work.  
I’m a bit embarrassed to post my code here, I’ve just learned all php by using kirby. It’s somehow glued together by all the tutorials in the documentation. I guess there are plenty ways to improve my code. Please feel free to comment 😊  
Thank you in advance

This is the template:

```
<section id="buch">
	<div id="Container" class="container">
		<ul>
		<?php $i = 0; foreach($articles as $article): ?>
		<li>
		<div class="flip mix <?php echo $article->art()->html() ?> <?php echo $article->kategorie()->html() ?> <?php echo $article->thema()->html() ?>"> 
			<div class="buch-datum"><time datetime="<?php echo $article->date('c', 'date') ?>"><?php echo $article->date('ymd', 'date') ?></time></div>
			<div class="buch-title"><a name="<?php echo $article->title()->html() ?>"><?php echo $article->title()->html() ?></a></div>
		</div>
		<div class="panel">
			<div class="slidescontainer">
			<div class="slidesjs" id="slides-<?= $i ?>">
				<?php foreach($article->images() as $image): ?>
				<img src="<?php echo $image->url() ?>">
				<?php endforeach ?>
			</div>
		</div>
		<div class="buch-wrapper">
			<span class="buch-uav"><span class="discription">UNTERTITEL</span><?php echo $article->untertitel()->html() ?></span>		
			<span class="buch-uav"><span class="discription">AUTOR</span>
				<a href="<?php echo $page->url() . '/autor:' . urlencode($article->autor()->html()) ?>"><?php echo $article->autor()->html() ?></a> 
				<?php echo $article->autor2()->html() ?>
			</span>
			<span class="buch-uav"><span class="discription">VERLAG</span>
				<a href="<?php echo $page->url() . '/verlag:' . urlencode($article->verlag()->html()) ?>"><?php echo $article->verlag()->html() ?></a>
			</span> 
			<span class="buch-sprache"><span class="discription">SPRACHE</span><?php echo $article->sprache() ?></span>
			<span class="buch-auflage"><span class="discription">AUFLAGE</span><?php echo $article->auflage()->html() ?></span>					
			<span class="buch-jahr"><span class="discription">JAHR</span><?php echo $article->jahr()->hmtl() ?></span>	
			<span class="buch-shop"><span class="discription">SHOP</span><?php echo $article->shop()->html() ?></span>
			<span class="buch-land"><span class="discription">LAND</span><?php echo $article->land()->html() ?></span>
			<span class="buch-preis"><span class="discription">PREIS</span><?php echo $article->preis()->html() ?></span>
			<span class="buch-gebraucht"><span class="discription">GEBRAUCHT</span>
				<?php if($article->gebraucht() == '1'): ?><span>✔</span><?php else: ?><span>—</span><?php endif ?>
			</span>
			<span class="buch-geschenk"><span class="discription">GESCHENK</span>
				<?php if($article->geschenk() == '1'): ?><span>✔</span><?php else: ?><span>—</span><?php endif ?>
			</span>
			<span class="buch-text"><span class="discription">INFO</span><?php echo $article->text()->html() ?></span>
			</div>
		<div class="buch-wrapper2">
			<span class="buch-art"><span class="discription">ART</span>
				<a href="<?php echo $page->url() . '/art:' . urlencode($article->art()->html()) ?>"><?php echo $article->art()->html() ?></a>
			</span>
			<span class="buch-kategorie"><span class="discription">KATEGORIE</span>
				<a href="<?php echo $page->url() . '/kategorie:' . urlencode($article->kategorie()->html()) ?>"><?php echo $article->kategorie()->html() ?></a>
			</span>
			<span class="buch-form"><span class="discription">FORM</span>
				<a href="<?php echo $page->url() . '/form:' . urlencode($article->form()->html()) ?>"><?php echo $article->form()->html() ?></a>
			</span>
			<span class="buch-thema"><span class="discription">THEMA</span>
				<a href="<?php echo $page->url() . '/thema:' . urlencode($article->thema()->html()) ?>"><?php echo $article->thema()->html() ?></a>
			</span>
			<span class="buch-gelesen"><span class="discription">GELESEN</span>
				<?php if($article->gelesen() == '1'): ?><span>✔</span><?php else: ?><span>—</span><?php endif ?>
			</span>
			<span class="buch-nummer"><span class="discription">ID</span>
				<time datetime="<?php echo $article->date('c', 'date') ?>"><?php echo $article->date('ymd', 'date') ?></time><?php echo $article->nummer()->html() ?>
			</span><span class="buch-typeface"><span class="discription">TYPEFACE</span>
				<a href="<?php echo $page->url() . '/typeface:' . urlencode($article->typeface()->html()) ?>"><?php echo $article->typeface()->html() ?></a>
			</span>
			<span class="buch-gestaltung"><span class="discription">GESTALTUNG</span>
				<a href="<?php echo $page->url() . '/gestaltung:' . urlencode($article->gestaltung()->html()) ?>"><?php echo $article->gestaltung()->html() ?></a>
			</span>
			<span class="buch-sammlung"><span class="discription">SAMMLUNG</span>
				<a href="<?php echo $page->url() . '/sammlung:' . urlencode($article->sammlung()->html()) ?>"><?php echo $article->sammlung()->html() ?></a>
			</span>
			<span class="buch-link"><span class="discription">LINK</span>
				<?php if(!$article->links()->empty()): ?>
				<ul>
				<?php foreach($article->links()->pages() as $item): ?>
				<li>
				<a href="#<?php echo $item->title()->html() ?>"><?php echo $item->title()->html() ?></a>
				</li>
				<?php endforeach ?>
				</ul>
				<?php endif ?>
			</span>	
			</div>
		</li>
		<?php $i++; endforeach ?>
		</ul>
	</div>	
</section>

```

This is the controller

```
    <?php

return function($site, $pages, $page) {

  // fetch the basic set of pages
  $articles = $page->children()->visible()->flip();

  // add the tag filter
  if ($sammlung = param('sammlung')) {
  	$articles = $articles->filterBy('sammlung', urldecode($sammlung), ',');
	};
  if($verlag = param('verlag')) {
 $articles = $articles->filterBy('verlag', urldecode($verlag), ',');
  };
  if($typeface = param('typeface')) {
 $articles = $articles->filterBy('typeface', urldecode($typeface), ',');
  };
  if($gestaltung = param('gestaltung')) {
 $articles = $articles->filterBy('gestaltung', urldecode($gestaltung), ',');
  };
  if($autor = param('autor')) {
 $articles = $articles->filterBy('autor', urldecode($autor), ',');
  };
  if($art = param('art')) {
 $articles = $articles->filterBy('art', urldecode($art), ',');
  };
 if($kategorie = param('kategorie')) {
 $articles = $articles->filterBy('kategorie', urldecode($kategorie), ',');
  };
 if($form = param('form')) {
$articles = $articles->filterBy('form', urldecode($form), ',');
  };
 if($thema = param('thema')) {
$articles = $articles->filterBy('thema', urldecode($thema), ',');
  };
  // fetch all tags
  $tags = $articles->pluck('tags', ',', false);

   return compact('articles', 'tags', 'tag', 'sammlung', 'typeface', 'verlag', 'gestalung', 'kategorie', 'art', 'thema', 'form');

};
```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:04pm UTC](https://forum.getkirby.com/t/fetching-tags-problem/2580/13 "2025-02-06T17:04:08Z")

</div>

Archived on February 6, 2025, 5:04pm UTC


