プラグインAdvanced custom fields(ACF)のacf/fields/relationship/resultフィルタの使用方法・解説

概要

acf/fields/relationship/result フィルタは、Advanced Custom Fields (ACF) プラグインでリレーションシップフィールドの結果をカスタマイズするために使用されます。このフィルタを使用することで、関係フィールドの各投稿に表示されるテキストをフィルタリングし、特定の条件や目的に応じた表示を実現できます。

よく使われる機能には、以下のような用途があります:

  1. 表示されるタイトルのカスタマイズ
  2. 投稿タイプに応じた条件分岐
  3. 特定のカスタムフィールドの値の表示
  4. 関連する投稿のリンクの変更
  5. 投稿の非表示(特定の条件に基づく)
  6. 表示するテキストのローカライズ(多言語対応)

構文

add_filter('acf/fields/relationship/result', 'my_acf_relationship_result', 10, 4);

パラメータ

  • $result (string): リレーションシップフィールドで表示される結果のテキスト
  • $post (WP_Post): リレーションシップにAddされた対象の投稿オブジェクト
  • $field (array): フィールドの設定配列
  • $selected (array): 選択済みの投稿IDの配列

戻り値

フィルタリングされた結果のテキストが返されます。

使用可能なバージョン

  • ACFバージョン: 5.0以降
  • WordPressバージョン: 4.0以降

この関数のアクションでの使用可能性

アクション名 使用例
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード

サンプル1:ポストタイトルをカスタマイズ

このコードは、リレーションシップフィールドで表示するポストタイトルの前に「関連ポスト:」というテキストを追加します。

add_filter('acf/fields/relationship/result', 'custom_relationship_result', 10, 4);
function custom_relationship_result($result, $post, $field, $selected) {
    return '関連ポスト:' . $result;
}

引用元: https://www.advancedcustomfields.com/resources/acf-fields-relationship/

サンプル2:特定のカスタムフィールドを付加

このサンプルでは、リレーションシップフィールドに表示される結果に特定のカスタムフィールドの値を加えます。

add_filter('acf/fields/relationship/result', 'add_custom_field_to_relationship', 10, 4);
function add_custom_field_to_relationship($result, $post, $field, $selected) {
    $custom_field_value = get_field('custom_field', $post->ID);
    return $result . ' (' . $custom_field_value . ')';
}

引用元: https://www.advancedcustomfields.com/resources/acf-fields-relationship/

サンプル3:投稿タイプに基づく条件分岐

このコードは、指定された投稿タイプのポストのみをリストに表示するための条件分岐を行います。

add_filter('acf/fields/relationship/result', 'filter_relationship_by_post_type', 10, 4);
function filter_relationship_by_post_type($result, $post, $field, $selected) {
    if (get_post_type($post) !== 'desired_post_type') {
        return '';
    }
    return $result;
}

引用元: https://www.advancedcustomfields.com/resources/acf-fields-relationship/

サンプル4:リレーションシップフィールドの非表示

特定の条件に基づいてリレーションシップフィールドのポストを非表示にするためのフィルタです。

add_filter('acf/fields/relationship/result', 'hide_specific_posts', 10, 4);
function hide_specific_posts($result, $post, $field, $selected) {
    if ($post->ID == 123) { // 投稿IDが123の場合
        return ''; // 非表示
    }
    return $result;
}

引用元: https://www.advancedcustomfields.com/resources/acf-fields-relationship/

サンプル5:多言語対応のカスタマイズ

このサンプルは、ポストタイトルを翻訳することを目的としています。

add_filter('acf/fields/relationship/result', 'translate_relationship_result', 10, 4);
function translate_relationship_result($result, $post, $field, $selected) {
    return __('Post Title: ', 'text-domain') . $result;
}

引用元: https://www.advancedcustomfields.com/resources/acf-fields-relationship/

この関数について質問する


上の計算式の答えを入力してください