LoginSignup
1
2

More than 5 years have passed since last update.

Movable Typeのプラグイン「SearchEntries」のテンプレート記述方法――タイトル、本文、続き、各カスタムフィールドの「いずれか」に含まれる「文字列(検索キーワード)」で記事検索するには?

Posted at

SearchEntriesプラグインとは

ブログ記事・ウェブページを文字列(検索キーワード)で検索したい時、MTの標準検索機能が対象とするフィールドは、たった4つなのです。

  • タイトル <$mt:EntryTitle$>
  • 本文 <$mt:EntryBody$>
  • 続き <$mt:EntryMore$>
  • キーワード <$mt:EntryKeywords$>

カスタムフィールドに登録した文字列も、キーワード検索の対象としたいですよね。
そんな問題を解決してくれるプラグインのひとつに、SearchEntriesプラグインがあります。

SearchEntriesダウンロードページ

SearchEntriesプラグインは、様々な条件下での検索をすることができます。
高機能ゆえ、ドキュメントが大変充実していますが、――納期が迫っていたりなんだりで――、ぱっと使わせていただきたい時に、どのようにテンプレートを書いて良いか、ドキュメントを精読しきれない場合もあるかと思います。

当方の備忘録も兼ねて、キーワード検索をしたい場合のテンプレートを投稿いたします。
SearchEntriesプラグインのインストール方法など、初期設定方法は、公式ドキュメントに詳細に記載されています。

SearchEntries解説ページ

SearchEntriesでの検索機能を補助するプラグイン

SearchEntriesでの検索機能を動作させるには、SearchEntriesプラグイン以外に、下記のプラグインも必要となります。

要件

  • ブログ記事を、タイトル、本文、続き、各カスタムフィールドの「いずれか」に含まれる「文字列(検索キーワード)」で検索したい
  • カスタムフィールドは以下のフィールドがあるとする
    • カスタムフィールド1 (ベースネーム: entry_cf_1)
    • カスタムフィールド2 (ベースネーム: entry_cf_2)
    • カスタムフィールド3 (ベースネーム: entry_cf_3)
  • 検索結果をページ分割したい

検索窓用テンプレート(インデックステンプレートまたはアーカイブテンプレートとして作成)

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="get" action="<$mt:RealtimeRebuildCGIPath$>" class="hd-utility_search clr">
<input type="hidden" name="blog_id" value="<$mt:BlogID$>">
<input type="hidden" name="tmpl_id" value="<$mt:GetTemplateID tmpl_name="詳細検索結果(キーワード検索)"$>">
<input type="hidden" name="first_time" value="1">
<input type="text" name="keyword" placeholder="キーワードを入力">
<button type="submit">検索</button>
</form>
</body>
</html>

検索結果画面用テンプレート(テンプレートモジュール「詳細検索結果(キーワード検索)」として作成)

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>

<$mt:TemplateNote value="▼▼検索を行う部分▼▼"$>
<$mt:GetQueryParamsToVars$>

<MTIGNORE>
// カスタムフィールド1の結果を変数「eid1」にセット
</MTIGNORE>
<mt:SearchConditions entry_ids="eid1">
    <mt:SearchConditionCFSection field="entry_cf_1">
        <$mt:SearchConditionCFLike values="$qp_keyword"$>
    </mt:SearchConditionCFSection>
</mt:SearchConditions>

<MTIGNORE>
// カスタムフィールド2の結果を変数「eid2」にセット
</MTIGNORE>
<mt:SearchConditions entry_ids="eid2">
    <mt:SearchConditionCFSection field="entry_cf_2">
        <$mt:SearchConditionCFLike values="$qp_keyword"$>
    </mt:SearchConditionCFSection>
</mt:SearchConditions>

<MTIGNORE>
// カスタムフィールド3の結果を変数「eid3」にセット
</MTIGNORE>
<mt:SearchConditions entry_ids="eid3">
    <mt:SearchConditionCFSection field="entry_cf_3">
        <$mt:SearchConditionCFLike values="$qp_keyword"$>
    </mt:SearchConditionCFSection>
</mt:SearchConditions>

<MTIGNORE>
// 検索結果ページ分割用の設定
// 「name="dp_per_page"」に区切り件数をセット(今回は1件区切り)
</MTIGNORE>
<mt:If name="qp_first_time">
    <$mt:SetVar name="count_only" value="dp_total_count"$>
    <$mt:SetVar name="dp_per_page" value="1"$>
    <$mt:SetVar name="dp_offset" value="0"$>
<mt:Else>
    <$mt:SetVar name="set_only" value="1"$>
    <$mt:DivPagesInit$>
</mt:If>

<MTIGNORE>
// mt:SearchConditionsの設定
// 「OR検索」なので「<$mt:SearchConditionUnion op="or"$>」を間に挟む
</MTIGNORE>
<mt:SearchConditions count_only="$count_only" set_only="$set_only">
    <mt:SearchConditionSection>
        <$mt:TemplateNote value="カスタムフィールド1の結果"$>
        <$mt:SearchConditionValues field="id" values="$eid1"$>
        <$mt:SearchConditionUnion op="or"$>
        <$mt:TemplateNote value="カスタムフィールド2の結果"$>
        <$mt:SearchConditionValues field="id" values="$eid2"$>
        <$mt:SearchConditionUnion op="or"$>
        <$mt:TemplateNote value="カスタムフィールド3の結果"$>
        <$mt:SearchConditionValues field="id" values="$eid3"$>
        <$mt:SearchConditionUnion op="or"$>
        <$mt:TemplateNote value="EntryTitleの結果"$>
        <$mt:SearchConditionLike field="title" values="$qp_keyword"$>
        <$mt:SearchConditionUnion op="or"$>
        <$mt:TemplateNote value="EntryBodyの結果"$>
        <$mt:SearchConditionLike field="text" values="$qp_keyword"$>
        <$mt:SearchConditionUnion op="or"$>
        <$mt:TemplateNote value="EntryMoreの結果"$>
        <$mt:SearchConditionLike field="text_more" values="$qp_keyword"$>
    </mt:SearchConditionSection>

<MTIGNORE>
// 検索結果ページ分割用の設定1
</MTIGNORE>
    <$mt:SearchLimit limit="$dp_per_page"$>
    <$mt:SearchOffset offset="$dp_offset"$>
</mt:SearchConditions>

<MTIGNORE>
// 検索結果ページ分割用の設定2
</MTIGNORE>
<mt:If name="qp_first_time">
    <mt:DivPagesInit per_page="$dp_per_page" total_count="$dp_total_count">
</mt:If>
<$mt:TemplateNote value="▲▲検索を行う部分▲▲"$>

<$mt:TemplateNote value="▼▼検索結果▼▼"$>
<mt:If name="qp_keyword">
<h1>キーワード「<$mt:Var name="qp_keyword"$>」の検索結果</h1>
<p><mt:If name="dp_total_count"><$mt:Var name="dp_total_count"$><mt:Else>0</mt:If>件</p>
</mt:If>
<$mt:LoadSearchedEntries$>
<mt:IfNonZero tag="SearchLoadedEntryCount">
    <$mt:BlogEntryCount setvar="blog_entry_count"$>
    <mt:Entries lastn="$blog_entry_count">
    <mt:EntriesHeader>
    <ul>
    </mt:EntriesHeader>
    <li><a href="<$mt:EntryPermalink$>"><$mt:EntryTitle escape="html"$></a></li>
    <mt:EntriesFooter>
    </ul>
    </mt:EntriesFooter>
    </mt:Entries>
<mt:Else>
    <mt:Unless name="qp_keyword">
        <p>検索キーワードを入れてください。</p>
    <mt:Else>
        <p>「<$mt:Var name="qp_keyword"$>」と一致する結果は見つかりませんでした。</p>
    </mt:Unless>
</mt:IfNonZero>
<$mt:TemplateNote value="▲▲検索結果▲▲"$>

<$mt:TemplateNote value="▼▼ページ分割▼▼"$>
<mt:DivPagesIfMulti>
<mt:SetVarBlock name="pager_link"$><$mt:DivPagesCGILink tmpl_name="詳細検索結果(キーワード検索)" build_params="1" name="keyword"$>&amp;page=</mt:SetVarBlock>
<mt:DivPagesIfPrevPage>
<p><a href="<$mt:GetVar name="pager_link"$><$mt:DivPagesPrevPageNumber$>">前に戻る</a></p>
</mt:DivPagesIfPrevPage>
<ol>
<mt:DivPagesLoop>
    <mt:DivPagesIfCurPage>
<li class="current"><a href="<$mt:GetVar name="pager_link"$><$mt:DivPagesNumber$>"><$mt:DivPagesNumber$></a></li>
    <mt:Else>
<li><a href="<$mt:GetVar name="pager_link"$><$mt:DivPagesNumber$>"><$mt:DivPagesNumber$></a></li>
    </mt:DivPagesIfCurPage>
</mt:DivPagesLoop>
</ol>
<mt:DivPagesIfNextPage>
<p><a href="<$mt:GetVar name="pager_link"$><$mt:DivPagesNextPageNumber$>">次へ進む</a></p>
</mt:DivPagesIfNextPage>
</mt:DivPagesIfMulti>
<$mt:TemplateNote value="▲▲ページ分割▲▲"$>

</body>
</html>

ポイント

カスタムフィールドの値を扱うには、条件の結果をいったん変数にセットし <$mt:SearchConditionValues$> として取り出します。

<MTIGNORE>
// カスタムフィールド1の結果を変数「eid1」にセット
</MTIGNORE>
<mt:SearchConditions entry_ids="eid1">
    <mt:SearchConditionCFSection field="entry_cf_1">
        <$mt:SearchConditionCFLike values="$qp_keyword"$>
    </mt:SearchConditionCFSection>
</mt:SearchConditions>
<mt:SearchConditions count_only="$count_only" set_only="$set_only">
    <mt:SearchConditionSection>
        <$mt:TemplateNote value="カスタムフィールド1の結果"$>
        <$mt:SearchConditionValues field="id" values="$eid1"$>
【中略】
    </mt:SearchConditionSection>
【中略】
</mt:SearchConditions>

ノンプログラマーでもMovable TypeとSearchEntriesを使えば、検索システム込みのウェブサイトが構築できるので楽しいです。
ぜひぜひコピペしてください~!

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2