LoginSignup
2
1

More than 3 years have passed since last update.

Advanced Custom Fields の関連記事を選択する際の並び順を変更

Posted at

WordPress で開発する時にあったら大変捗るプラグインといえば Advanced Custom Fields です。
その中のフィールドの一つである「関連」フィールドの Tips をご紹介します。

関連フィールドとは

https://www.advancedcustomfields.com/resources/relationship/
作成済みの記事やページ(カスタム投稿も可能)を選択できる機能を提供してくれます。ある記事に対して手動で関連記事を設定して有用なサイト内回遊を促したり、固定ページで特定の記事を表示させたい場合などに大変便利です。

選択可能な記事の表示順を変更する

ただこの記事選択をする時のUI上の記事の並び順がタイトルのアイウエオ順となっており、キーワード検索を前提とした場合は良いと思うのですが、基本的に最新の記事から記事を選択したい場合などでは不便なので、公開日時の新しい順に並び替えたいと思います。

Screen Shot 2020-05-01 at 18.35.14.png
▲ この記事の順番を変更したい。

https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
ACF にはフィルターも豊富に用意されています。今回は上記のフィルターを使って並び順を設定します。

functions.php
function my_relationship_query( $args, $field, $post_id ) {

  // only show children of the current post being edited
  $args['orderby'] = 'date'; //日時での並び替え
  $args['order'] = 'DESC'; // 降順

  // return
  return $args;

}
// filter for every field
add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);
2
1
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
2
1