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?