在使用ACF更新/创建的自定义帖子类型后,在Wordpress中调用函数



每次自定义帖子类型发生变化时,我都想调用一个函数。发布、更新或删除。在该函数中,我从该自定义帖子类型中获取所有帖子,并创建一个 json 文件并将其导出到文件中。

add_action( 'transition_post_status', 'get_resources_data', 10, 3 );
function get_resources_data($new_status, $old_status, $post ) {
if ($post->post_type == 'resources') {
$args = array (
'post_type' => 'resources',
'post_status' => 'publish',
'posts_per_page' => -1
);
$queryResults = new WP_Query( $args );
if ( $queryResults->have_posts() ) {
//do my stuff here
//fetch acf fields with get_field()
//create json file
//export json file
}
}
}

问题是自定义帖子类型有一些高级自定义字段,我将其包含在 JSON 文件中。但是,创建新帖子时,所有 ACF 均为空,而标题和创建数据等字段可用。如果我更新帖子,则会获取所有 ACF。

我的印象是,在 ACF 存储在数据库中之前,transition_post_status就被钩住了。我应该使用其他操作还是使用其他方式?

ACF实际上为您提供了一个动作钩子。

add_action('acf/save_post', 'get_resources_data');- 如果将优先级设置为低于 10,则在保存数据之前应用操作,如果发出 prio,或者使其高于 10,则在保存数据后应用该操作。

你可以在这里阅读更多关于钩子的信息:https://www.advancedcustomfields.com/resources/acf-save_post/

最新更新