AT9*_*T92 4 php database wordpress advanced-custom-fields
我设置了一些 ACF 字段的自定义帖子类型。我还设置了 ACF 选项页面。
当选项页面更新时,我试图使用选项页面内文本字段中的值来更新所有自定义帖子上的文本字段。
这是我尝试过的:
function update_global_flash_text(){
$current_page = get_current_screen()->base;
if($current_page == 'toplevel_page_options') {
function update_global_servicing_text() {
$args = array(
'post_type' => 'custom',
'nopaging' => true,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
update_field('servicing_flash_text', $_POST['global_servicing_offer_text']);
}
}
wp_reset_postdata();
}
if(array_key_exists('post',$_POST)){
update_global_servicing_text();
}
}
}
add_action('admin_head','update_global_flash_text');
Run Code Online (Sandbox Code Playgroud)
理想情况下,我还想仅在全局字段值发生更改时更新 posts 字段。
您可能正在寻找钩子acf/save_post。每当保存 ACF 选项页面时都会触发此操作。只需确保当前屏幕具有您的选项页面的 ID。
function my_function() {
$screen = get_current_screen();
/* You can get the screen id when looking at the url or var_dump($screen) */
if ($screen->id === "{YOUR_ID}") {
$new_value = get_field('global_servicing_offer_text', 'options');
$args = array(
'post_type' => 'custom',
'nopaging' => true,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
update_field('servicing_flash_text', $new_value);
}
}
wp_reset_postdata();
}
}
add_action('acf/save_post', 'my_function');
Run Code Online (Sandbox Code Playgroud)
这对你有帮助吗?
编辑:由于您要求仅在全局值发生更改时更新数据,因此您应该执行以下操作:
1 为您的acf/save_post操作指定高于 10 的优先级:
add_action('acf/save_post', 'my_function', 5);
Run Code Online (Sandbox Code Playgroud)
2 获取旧值和新值:
$old_value = get_field('global_servicing_offer_text', 'options');
// Check the $_POST array to find the actual key
$new_value = $_POST['acf']['field_5fd213f4c6e02'];
Run Code Online (Sandbox Code Playgroud)
3 比较它们if($old_value != $new_value)
| 归档时间: |
|
| 查看次数: |
2705 次 |
| 最近记录: |