我在 WordPress 網站的前端使用 ACF 表單。“帖子 A”中的 ACF 表單創建了一個新帖子“帖子 B”。我正在嘗試創建一個函式來更新帖子 A 中的 ACF 欄位(然后我將使用它從帖子 A 中洗掉表單,以便它只能提交一次)。我一直在嘗試使用 acf/save_post 操作來更新欄位,但這似乎只影響帖子 B 而不是帖子 A。這是我的代碼:
<?php
add_action('acf/save_post', 'update_post_status', 20);
function update_post_status( $post_id ) {
if( get_post_type($post_id) !== 'mypost' ) {
return;
}
update_field('form_submitted', 'yes');
}
?>
uj5u.com熱心網友回復:
我認為您在此任務中使用了不正確的鉤子
acf-save_post
Fires when saving the submitted $_POST data.
您可以使用“ save_post ”
Save_post is an action triggered whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. The data for the post is stored in $_POST, $_GET or the global $post_data, depending on how the post was edited. For example, quick edits use $_POST.
例如
add_action( 'save_post', 'my_save_post_function', 10, 3 );
function my_save_post_function( $post_ID, $post, $update ) {
if( get_post_type($post_ID) !== 'mypost' ) {
return;
}
update_field('form_submitted', 'yes', $post_ID);
}
uj5u.com熱心網友回復:
我已經成功使用acf/validate_save_post而不是acf/save_post在創建新帖子之前觸發,因此該函式仍然參考原始帖子。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/366684.html
標籤:php WordPress的 高级自定义字段 活性炭
