使用 ACF 自定义 post 类型 updated/created 后在 Wordpress 中调用函数
Call function in Wordpress after a custom post type updated/created with ACF
每次自定义 post 类型发生变化时,我都想调用一个函数。发布、更新或删除。在该函数中,我从该自定义 post 类型中获取所有 posts 并创建一个 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
}
}
}
问题是自定义 post 类型有一些我包含在 JSON 文件中的高级自定义字段。但是,当创建一个新的post时,所有的ACF都是空的,而标题和创建数据等字段是可用的。如果我更新 post,将获取所有 ACF。
我的印象是 transition_post_status
在 ACF 存储到数据库之前就被挂钩了。我应该使用其他操作还是以其他方式执行?
ACF 实际上为您提供了一个动作挂钩。
add_action('acf/save_post', 'get_resources_data');
- 如果您将优先级设置为低于 10,则在保存数据之前应用操作,如果您发出优先级,或将其设置为高于 10,则在保存数据后应用.
您可以在此处阅读有关挂钩的更多信息:https://www.advancedcustomfields.com/resources/acf-save_post/
每次自定义 post 类型发生变化时,我都想调用一个函数。发布、更新或删除。在该函数中,我从该自定义 post 类型中获取所有 posts 并创建一个 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
}
}
}
问题是自定义 post 类型有一些我包含在 JSON 文件中的高级自定义字段。但是,当创建一个新的post时,所有的ACF都是空的,而标题和创建数据等字段是可用的。如果我更新 post,将获取所有 ACF。
我的印象是 transition_post_status
在 ACF 存储到数据库之前就被挂钩了。我应该使用其他操作还是以其他方式执行?
ACF 实际上为您提供了一个动作挂钩。
add_action('acf/save_post', 'get_resources_data');
- 如果您将优先级设置为低于 10,则在保存数据之前应用操作,如果您发出优先级,或将其设置为高于 10,则在保存数据后应用.
您可以在此处阅读有关挂钩的更多信息:https://www.advancedcustomfields.com/resources/acf-save_post/