WordPress save_post_map动作发布时失效,自定义字段获取异常
问题
我使用Pods创建了名为“map”的自定义文章类型,需求是在map文章创建、更新或移入回收站时更新一个JSON文件。当前代码在更新(UPDATE)或移入回收站(TRASH)时运行正常,但发布(PUBLISH)文章时出现异常:函数虽执行,但无法正确从自定义字段map_date中获取数据,日期始终返回1970年1月1日,不过特色图片URL显示正常。点击更新按钮后,函数再次执行,JSON数据可正确填充。
相关代码:
function generate_my_json($post_ID) { // Check if the post type is 'satellite_collars_ma' if (get_post_type($post_ID) !== 'map') { return; } $map_entries = array(); $args = array( 'post_type' => 'map', 'posts_per_page' => -1, ); $map_query = new WP_Query($args); while ($map_query->have_posts()) { $map_query->the_post(); $map_date = get_post_meta(get_the_ID(), 'map_date', true); // Customize the date format if needed $start_date = array( 'month' => date('m', strtotime($map_date)), 'day' => date('d', strtotime($map_date)), 'year' => date('Y', strtotime($map_date)), ); // Customize the featured image URL based on your setup $featured_image_url = get_the_post_thumbnail_url(get_the_ID(), 'full'); // Customize this part to structure your JSON as needed $entry_data = array( 'media' => array( 'url' => $featured_image_url, ), 'start_date' => $start_date , ); $map_entries[] = $entry_data; } wp_reset_postdata(); $json_data = json_encode(array('events' => $map_entries), JSON_PRETTY_PRINT); // Customize the path where you want to save the JSON file $json_file_path = ABSPATH . 'wp-content/map-json/my-feed.json'; file_put_contents($json_file_path, $json_data); } add_action('save_post_map', 'generate_my_json', 10, 1); add_action('wp_trash_post', 'generate_my_json', 10, 1); add_action('untrashed_post', 'generate_my_json', 10, 1);
发布后的JSON内容:
{ "events": [ { "media": { "url": "https://mysite.com/wp-content/uploads/2024/05/20240516-map.jpg" }, "start_date": { "month": "01", "day": "01", "year": "1970" } } ] }
更新后的JSON内容:
{ "events": [ { "media": { "url": "https://mysite.com/wp-content/uploads/2024/05/20240516-map.jpg" }, "start_date": { "month": "05", "day": "22", "year": "2024" } } ] }
解决方案
问题原因
首次发布map类型文章时,save_post_map钩子默认优先级(10)触发时机早于Pods保存自定义字段map_date到数据库的时间,此时get_post_meta无法获取刚提交的日期值,传入strtotime的是空字符串,返回时间戳0,对应1970年1月1日。而更新文章时,map_date已存在于数据库中,所以能正常读取。
修复方法
将save_post_map钩子的优先级调高,确保函数在Pods完成自定义字段保存后再执行:
修改钩子注册代码,把优先级从10改为20:
add_action('save_post_map', 'generate_my_json', 20, 1); add_action('wp_trash_post', 'generate_my_json', 10, 1); add_action('untrashed_post', 'generate_my_json', 10, 1);
额外优化(可选)
如果担心优先级调整后仍有问题,可以在函数中针对当前操作的帖子,优先从提交的表单数据中读取map_date,同时增加空日期容错处理:
function generate_my_json($post_ID) { if (get_post_type($post_ID) !== 'map') { return; } $map_entries = array(); $args = array( 'post_type' => 'map', 'posts_per_page' => -1, ); $map_query = new WP_Query($args); while ($map_query->have_posts()) { $map_query->the_post(); $current_post_id = get_the_ID(); // 优先从POST数据读取当前操作帖子的日期(适配刚发布的场景) if ($current_post_id === $post_ID && isset($_POST['map_date'])) { $map_date = sanitize_text_field($_POST['map_date']); } else { $map_date = get_post_meta($current_post_id, 'map_date', true); } // 处理空日期的情况,避免出现1970年的默认值 $start_date = array( 'month' => '00', 'day' => '00', 'year' => '0000', ); if (!empty($map_date) && strtotime($map_date) !== false) { $start_date = array( 'month' => date('m', strtotime($map_date)), 'day' => date('d', strtotime($map_date)), 'year' => date('Y', strtotime($map_date)), ); } $featured_image_url = get_the_post_thumbnail_url($current_post_id, 'full'); $entry_data = array( 'media' => array( 'url' => $featured_image_url, ), 'start_date' => $start_date , ); $map_entries[] = $entry_data; } wp_reset_postdata(); $json_data = json_encode(array('events' => $map_entries), JSON_PRETTY_PRINT); $json_file_path = ABSPATH . 'wp-content/map-json/my-feed.json'; file_put_contents($json_file_path, $json_data); }
内容的提问来源于stack exchange,提问作者Nikolai
相关产品推荐
相关产品推荐

