[WordPress] 外掛分享: ACF Post-2-Post

前言介紹

  • 這款 WordPress 外掛「ACF Post-2-Post」是 2017-01-01 上架。
  • 目前有 3000 個安裝啟用數。
  • 上一次更新是 2025-04-15,距離現在已有 19 天。
  • 外掛最低要求 WordPress 4.0 以上版本才可以安裝。
  • 有 39 人給過評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

hube2 |

外掛標籤

acf | bidirectional relationship |

內容簡介

這是 Advanced Custom Fields (ACF) 的插件附加程式,要求版本至少為 5。

此插件不適用於 ACF 版本 4。

如果未安裝 ACF >= 5,此插件將不提供任何功能。***

此插件不會創建新的欄位類型或任何管理介面。當使用下述
所述時,此插件可以使現有的 ACF 關聯和文章物件欄位雙向工作,
自動更新關聯的另一端的關聯欄位。

有關詳細信息,請參閱其他說明

Github 存儲庫

此插件還可在 GitHub 上找到
https://github.com/Hube2/acf-post2post

使用方式

創建關聯或文章物件欄位。
該欄位必須在最上層。它不能是重複器或靈活內容欄位的子欄位。
所有文章上的欄位名稱必須相同。換句話說,如果您想使不同的文章類型相關聯,則必須在兩個文章類型上添加具有相同欄位名稱的欄位。

當您將文章添加到關聯或文章物件欄位中,並且在關聯的文章中出現相同的欄位名稱時,與編輯的文章建立關聯的關聯欄位將自動更新。

如果從關聯中刪除文章,則也會更新要編輯的文章的關聯。

文章物件欄位

如果正在使用文章物件欄位

如果它允許多個值,則將以與關聯欄位相同的方式工作。
如果它不允許多個值且關聯文章已包含一個值,請參見重寫設置。

重寫設置

如果相關文章中的欄位,無論它是只允許1個值的文章物件欄位,還是具有最大關聯文章數量的關聯欄位,如果相關文章中的欄位已經有最大數目的值,則默認情况下不會添加新值。 您可以通過指定重寫設置來覆蓋此默認設置。

如何添加重寫設置

add_filter('acf-post2post/overwrite-settings', 'my_overwrite_settings');
function my_overwrite_settings($settings) {
$settings['field_name'] = array(
'overwrite' => true,
'type' => 'first'
);
return $settings;
}

$settings 數組的每個元素都是一個數組。數組的索引是要指定設置的欄位。每個欄位可以有2個引數。

overwrite:true/false 或 1/0。如果設置為 true 或 1,則新值將覆蓋舊值。此設置的默認值為 false。
type:‘first’ 或 ‘last’。應刪除現有值中的哪一個,添加第一個還是最後一個。默認值為‘first’。

從現有列表中刪除值後,新值將添加到列表末尾。

欄位例外

您可以使用篩選器禁用特定欄位鍵的自動雙向關聯

// field_XXXXXXXX= 您要禁用雙向關聯的欄位鍵
add_filter('acf/post2post/update_relationships/key=field_XXXXXXXX', '__return_false');

更新後鉤子

在更新文章並傳遞單個文章 ID 後,可以使用兩個動作。請確認您查看微妙的差異

原文外掛簡介

This is an add on plugin for Advanced Custom Fields (ACF) >= Version 5.
This plugin will not work with ACF Version 4.
This plugin will not provide any functionality if ACF >=5 is not installed.***
This plugin does not create a new type of field or any admin interface. This plugin when used as
explained below makes the existing ACF Relationship and Post Object fields work bi-directionaly,
automatically updating the relationship field on the other end of the relationship.
For more information see Other Notes
Github Repository
This plugin is also on GitHub
https://github.com/Hube2/acf-post2post
How To Use

Create a relationship or post object field.
The field must be at the top level. It cannot be a subfield of a repeater or a flexible content field.
The field name must be the same on all posts. In other words if you want to have different post types be related then you must add a field with the same field name on both post types.

When you add a post to a relationship or post object field and the same field name appears on the post added to the relationship then the relationship field on the related post will be updated to include a relationship to the post being edited.
If a post is removed from a relationship then the post being removed will also be updated to remove the relationship to the post being edited.
Post Object Fields
If a post object field is being used

If it allows multiple values then it will work the same way that relationship fields work.
If it does not allow multiple values and the related post already contains a value see Overwrite Settings

Overwrite Settings
If the field in a related post, whether it is a post object field that only allows 1 value or a relationship field that has a maximum number of related posts, if the field in the related post already has the maximum number of values allowed, by default, a new value will not be added. You can override this default by specifying overwrite settings.
How to add overwrite settings
add_filter('acf-post2post/overwrite-settings', 'my_overwrite_settings');
function my_overwrite_settings($settings) {
$settings['field_name'] = array(
'overwrite' => true,
'type' => 'first'
);
return $settings;
}

Each element of the $settings array is an array. The index of the array is the field that you want to specify settings for. Each field can have 2 arguments.

overwrite: true/false or 1/0. If set to true or 1 then new values will overwrite older values. The default value of this setting is false.
type: ‘first’ or ‘last’. Which of the existing values should be removed, the first one added or the last. The default value is ‘first’.

after a value is removed from the existing list the new value is added to the end of the list.
Field Exeptions
You can disable automatic bidirectional relationships for specific field keys using the filter
// field_XXXXXXXX = the field key of the field
// you want to disable bidirectional relationships for
add_filter('acf/post2post/update_relationships/key=field_XXXXXXXX', '__return_false');

After update hooks
There are two actions that can be used after a post is updated and passes a single post ID. Please make sure you see the subtle difference in these two hooks.
The first is run after each related post is updated
add_action('acf/post2post/relationship_updated', 'my_post_updated_action');
function my_post_updated_action($post_id) {
// $post_id == the post ID that was updated
// do something after the related post is updated
}

The second is run after all posts are updated and passes an array of post IDs.
add_action('acf/post2post/relationships_updated', 'my_post_updated_action');
function my_post_updated_action($posts) {
// $posts == and array of post IDs that were updated
// do something to all posts after update
foreach ($posts as $post_id) {
// do something to post
}
}

各版本下載點

  • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
  • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「ACF Post-2-Post」來進行安裝。

(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


1.2.3 | 1.2.4 | 1.2.5 | 1.2.6 | 1.2.7 | 1.2.8 | 1.3.0 | 1.3.1 | 1.3.2 | 1.4.0 | 1.4.1 | 1.5.0 | 1.5.1 | 1.5.2 | 1.6.0 | 1.7.0 | trunk |

延伸相關外掛(你可能也想知道)

  • Advanced Custom Fields (ACF®) 》Advanced Custom Fields 可以讓 WordPress 網站成為一個完整的內容管理系統,提供您所有工具以更好管理您的數據。, 使用 Advanced Custom Fields 外掛,完全...。
  • Advanced Custom Fields: Font Awesome Field 》在 Advanced Custom Fields 中添加一個Font Awesome 圖標字段類型。, , 指定要使用哪些 Font Awesome...。
  • ACF Content Analysis for Yoast SEO 》此外掛確保 Yoast SEO 分析包括所有 ACF 內容,包括彈性內容和重複器。, Yoast SEO for WordPress 內容和 SEO 分析不會考慮一篇文章的Advanced Custom Fields...。
  • Advanced Custom Fields: Extended 》🚀 全方位增強套件,可改進WordPress和Advanced Custom Fields。此外掛旨在提供一個強大的管理框架,涵蓋了眾多改進和優化。, 此外掛需要至少 ACF Pro 5.8。,...。
  • ACF Photo Gallery Field 》t; 'Status', , 'name' => 'status', , 'choices' => array(, 'public' => 'Public', , 'private' => ...。
  • Table Field Add-on for ACF and SCF 》表格欄位外掛程式增強了Advanced Custom Fields外掛程式的功能,讓表格編輯更易於使用。, 此外掛程式需要Advanced Custom Fields外掛程式或專業版!, 表格欄...。
  • Really Simple CSV Importer 》替代 CSV 導入外掛程式。簡單而強大,最適合於技術狂熱者。, , 類別支援, 標籤支援, 自訂欄位支援, Smart Custom Fields 支援, Custom Field Suite 支援, Adv...。
  • ACF to REST API 》此 WordPress 外掛在WordPress REST API中提供了Advanced Custom Fields的端點, 詳細資訊請參閱GitHub:https://github.com/airesvsg/acf-to-rest-api/。
  • ACF Quick Edit Fields 》這個 WordPress 外掛為 Advanced Custom Fields Plugin(Pro 和 Free 5+)增加了快速編輯功能。, 特色, , 在清單檢視中顯示 ACF 欄位值, 支援的 ACF 欄位類型...。
  • Advanced Custom Fields: Gravity Forms Add-on 》提供進階自訂欄位 (Advanced Custom Field),讓 WordPress 編輯使用者或管理員可以在欄位群組配置中選擇 Gravity Form。, 可以在 外掛的 GitHub 存儲庫 中找...。
  • Advanced Custom Fields: Image Aspect Ratio Crop Field 》這是一個進階自訂欄位 (Advanced Custom Fields) 的外掛,會在使用者上傳圖片後強制執行特定的長寬比例或像素大小裁剪。使用長寬比例對於回應式圖片的使用情...。
  • Advanced Custom Fields: Nav Menu Field 》使用 Nav Menu Field 外掛將導覽選單加入Advanced Custom Fields (ACF)。此外掛會在 ACF(版本 5 和 4)加入 Nav Menu 欄位類型,讓您從 WordPress 管理後台...。
  • ACF Theme Code for Advanced Custom Fields 》ACF Theme Code 外掛可以自動生成在主題中實現 Advanced Custom Fields 所需的程式碼。以下是該外掛的功能特色:, , - 縮短網站開發時間。, - 當您發布、編輯...。
  • ACF qTranslate 》此外掛提供 qTranslate-X 相容的 ACF4 和 ACF5PRO 欄位類型,包括文字、文字區域、所見即所得編輯器、影像和檔案。新增欄位至群組時,這些新的欄位類型將在欄...。
  • ACF Options For Polylang 》您使用了高級自訂欄位來建立選項頁面,並且安裝了 Polylang 外掛以獲得令人驚嘆的多語言網站嗎?, 可惜的是,Polylang 並不處理 ACF 的選項頁面。這意味著您...。

文章
Filter
Apply Filters
Mastodon