Reputation: 7577
I want to call a function every time there is a change in a custom post type. Either publish, update or delete. In that function, I fetch all posts from that custom post type and create a json file that I export in a file.
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
}
}
}
The problem is that the custom post type has a few advanced custom fields which I include in the JSON file. However, when a new post is created, all ACF are null, while fields such as title and creation data are available. If I update a post, all ACF are fetched.
My impression is that transition_post_status
is hooked before the ACF are stored in the Database. Should I use another action or do it with another way?
Upvotes: 2
Views: 2226
Reputation: 2492
ACF actually supplies you with an action hook for exactly just that.
add_action('acf/save_post', 'get_resources_data');
- if you set the priority to below 10, the action is applied before the data is saved, if you emit the prio, or have it above 10, it is applied after the data is saved.
You can read more about the hook here : https://www.advancedcustomfields.com/resources/acf-save_post/
Upvotes: 3