SearchContentsプラグイン(その11・検索結果を並べ替える)

SearchContentsプラグインでは、検索結果をフィールドの値で並べ替えることもできます。
ただし、Movable Typeのコアの書き換えが必要になります。

1.コアの書き換え

まず、Movable Typeのコアのソースコードを書き換えます。
Movable Typeのディレクトリの中のlib/MT/ObjectDriver/Driver/DBI.pmファイルで、555行目付近を以下のように書き換えます。

書き換え前

## Set statement's ORDER clause if any.
if ( $args->{sort} || $args->{direction} ) {
    my $order = $args->{sort} || 'id';
    if ( !ref($order) ) {
        my $dir = $args->{direction}
            && $args->{direction} eq 'descend' ? 'DESC' : 'ASC';
        $stmt->order(
            {   column =>
                    $dbd->db_column_name( $tbl, $order, $alias ),
                desc => $dir,
            }
        );
    }
    else {
        my @order;
        foreach my $ord (@$order) {
            push @order,
                {
                column => $dbd->db_column_name(
                    $tbl, $ord->{column}, $alias
                ),
                desc => $ord->{desc},
                };
        }
        $stmt->order( \@order );
    }
}

書き換え後(ハイライトの行を追加/変更)

## Set statement's ORDER clause if any.
if ( $args->{sort} || $args->{direction} ) {
    my $order = $args->{sort} || 'id';
    my $tmp_tbl = $args->{sort_class} ? $driver->table_for($args->{sort_class}) : $tbl;
    if ( !ref($order) ) {
        my $dir = $args->{direction}
            && $args->{direction} eq 'descend' ? 'DESC' : 'ASC';
        $stmt->order(
            {   column => $args->{sort_alias}
                    ? $dbd->db_column_name( $tmp_tbl, $order, $args->{sort_alias} )
                    : $dbd->db_column_name( $tbl, $order, $alias ),
                desc => $dir,
            }
        );
    }
    else {
        my @order;
        foreach my $ord (@$order) {
            my $tmp_tbl = $ord->{sort_class} ? $driver->table_for($ord->{sort_class}) : $tbl;
            push @order,
                {
                column => $ord->{sort_alias}
                    ? $dbd->db_column_name(
                          $tmp_tbl, $ord->{column}, $ord->{sort_alias}
                      )
                    : $dbd->db_column_name(
                          $tbl, $ord->{column}, $alias
                      ),
                desc => $ord->{desc},
                };
        }
        $stmt->order( \@order );
    }
}

2.MTSearchContentSortタグ

並べ替えを指定するには、MTSearchContentSortというテンプレートタグを使います。

コンテンツフィールドの値で並べ替えるには、content_fieldモディファイアで、コンテンツフィールドの名前(またはID/ユニークID)を指定します。
共通フィールドの値で並べ替えるには、fieldモディファイアでフィールド名を指定します。
また、検索結果を降順に並べ替えるには、「sort_order="descend"」のモディファイアも付加します。

複数のフィールドで並べ替えたい場合は、MTSearchContentSortタグを複数回書きます。
1つ目のMTSearchContentSortタグで指定したフィールドで、最初の並べ替えを行います。
その際に、そのフィールドの値が同じになっているコンテンツデータが複数ある場合に、2つ目のMTSearchContentSortタグで指定したフィールドで並べ替えを行います。

例えば、「商品」というコンテンツタイプがあり、「単価」という数値型のコンテンツフィールドを定義しているものとします。
この状態で、まず単価の安い順に並べ替え、単価が同じものは公開日(authored_on)フィールドの新しい順(降順)で並べ替えたいとします。
この場合、以下のようにテンプレートを組みます。

<mt:SearchContents content_type="商品">
  検索条件を表すテンプレートタグ
  <$mt:SearchContentSort content_field="単価"$>
  <$mt:SearchContentSort field="authored_on" sort_order="descend"$>
</mt:SearchContents>

3.SearchContentsプラグイン関係の記事の一覧