2019/4/15

Craft CMS で特定のタグがついているものを除外して記事リストを作成する #craftcms

Craft CMS で特定のタグがついてるエントリ以外をリストアップしたいなー、とおもって調べてみるもよくわからず。

毎度毎度の @BUN に教えてもらって試してみた。

とりあえず完成のコード

{# Diary という名前のタグをセット #}
{% set tagDiary = craft.tags().title('Diary').one() %}
{# Diary タグと関連づいているエントリ ID を取得 #}
{% set diaryEntryIds = craft.entries.section('article').relatedTo(tagDiary).ids() %}
{# 上記 ID を連結 #}
{% set excludeEntryIds = diaryEntryIds | join(', not ') %}
{# それらを除外した残りのエントリを取得 #}
{% set filterdEntries = craft.entries.section('article').id('and, not ' ~ excludeEntryIds).limit(null).all() %}
{% for entry in filterdEntries %}

ループ処理 {% endfor %}

こんな感じで。

記事にタグを紐づけているけど、実態としてはリレーションとして扱われる。

リレーション | Craft 3 ドキュメント
https://docs.craftcms.com/v3/j…

MT脳だったから、タグはデフォルトのフィールドだったっけ?とか思ってたりしたけど、そもそもタグの設定はちょっとだけ複雑だったのを思い出す。

タグのフィールド関係の設定

こんな感じでタグのソースをまず作る

その上でフィールドを作って、タグのソースを選択する。

そうそうそうでしたね、、、、というかんじで。

コードを見直す

まずこの部分。

{# Diary タグと関連づいているエントリ ID を取得 #} {% set diaryEntryIds = craft.entries.section(‘article’).relatedTo(tagDiary).ids() %}

ids() はマッチするIDのリストを返す。

エレメントクエリについて | Craft 3 ドキュメント
https://docs.craftcms.com/v3/j…

次にこの部分。

{# 上記 ID を連結 #} {% set excludeEntryIds = diaryEntryIds | join(’, not ’) %}

このjoinでこんな感じになってるってことか。

$id , not $id , not $id

join - Documentation - Twig - The flexible, fast, and secure PHP template engine
https://twig.symfony.com/doc/2…

んでエントリを取り出す。

{# それらを除外した残りのエントリを取得 #} {% set filterdEntries = craft.entries.section(‘article’).id(‘and, not ’ ~ excludeEntryIds).limit(null).all() %}

この部分

.id(‘and, not ’ ~ excludeEntryIds)

.id(and, not <$id

.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id
.id(and, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)
gt;, not <$id#####replaceparse8#####gt;, not <$id#####replaceparse8#####gt;)

こんな感じになってるってことかな。


チルダ

~

は連結。

Twig for Template Designers - Documentation - Twig - The flexible, fast, and secure PHP template engine
https://twig.symfony.com/doc/2…


とりあえず、id の and のところがちょっとよくわからないな。

あとで聞いてみよう。

参考

一覧とかでの絞り込みとかは @bun のこちらのエントリが参考になる

Craft CMS で日付や数値を指定してエントリを絞り込む | BUN:Log
https://bunlog.dreamseeker.dev…