首页 > 解决方案 > 循环浏览页面并为函数内部的每个内部更新 ACF 字段数据

问题描述

有点问题,希望得到帮助。基本上,我正在触发一个acf/save_post名为“设置”的自定义帖子类型的操作功能。因此,如果保存或更新了“设置”,我想遍历所有现有页面并使用特定数据更新每个页面的 ACF 字段。我的循环在下面。我整个周末都在和这个东西作斗争,但它似乎没有更新任何页面。任何帮助都会很大!谢谢!

$the_query = new WP_Query( array( 'post_type' => 'page' ) );

while ( $the_query->have_posts() ) : $the_query->the_post();
    global $post;  
    $pageID = $post->ID;
    update_field('temp_post_data','test' , $pageID);
endwhile;

标签: phpwordpressfunctionloopsadvanced-custom-fields

解决方案


您正在尝试更新当前页面上的字段。

尝试这个

function my_custom_function() {
  $pages = get_pages(); // get all pages
  foreach($pages as $page):
    // error_log($page->ID); // debug if needed
    update_field('temp_post_data','test' , $page->ID); //for each page we are updating  field
  endforeach;
}

推荐阅读