php - 循环浏览页面并为函数内部的每个内部更新 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;
解决方案
您正在尝试更新当前页面上的字段。
尝试这个
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;
}
推荐阅读
- google-apps-script - googleappscript 中的错误
- node.js - 为什么我的 express-server 仅通过浏览器支持 https 而不是通过其他 nodejs 脚本?
- java - 无法使用intellij制作两个独立的jar文件
- javascript - HTML/JS/JSON- 获取用户输入并将其作为 JSON 对象添加到 JSON 有效负载
- c# - 如何在 C# 中停止和开始重新监听客户端套接字连接
- ios - 我的自定义相册收藏视图加载速度很慢
- android - 如何实现位置搜索?
- jquery - 这个 npm 包可以导入吗?
- android - 如何知道安卓设备的年龄?
- python - Matplotlib 填充权重的阶梯图