以下是您可以做的:
当帖子的状态从future
到publish
, 当预定时间到达时就会发生。然后,将其状态更改为draft,并向API发出请求。如果API回复了,您可以存储回复的内容并发布帖子。如果没有,请再次安排。
function api_request_transition( $new_status, $old_status, $post ) {
// Let\'s check for draft too, since we will set the status to draft if the API doesn\'t reply
if ( ($old_status ==\'future\' || $old_status ==\'draft\') && $new_status ==\'publish\' ) {
// Set the status to draft for now
$current_post = array(
\'ID\' => $post->ID,
\'post_title\' => $post->post_title,
\'post_status\' => \'draft\',
);
wp_update_post( $current_post );
// Get the content from the API
$api_data = file_get_contents(\'URL HERE\');
// If the content is valid, add it to the post and publish it
if ($api_data) {
$my_post = array(
\'ID\' => $post->ID,
\'post_title\' => $post->post_title,
\'post_content\' => $api_data,
\'post_status\' => \'publish\',
);
wp_update_post( $my_post );
// If the API didn\'t answer, run this process again in 5 minutes
} else {
// Pass the data to the cron job
$args = (
$new_status,
$old_status,
$post ,
);
// Schedule this again for 5 minutes later
wp_schedule_single_event(time() + 300 , \'api_repeat_request\', $args);
}
}
}
add_action( \'transition_post_status\', \'api_request_transition\', 10, 3 );
add_action( \'api_repeat_request\', \'api_request_transition\',1 ,3 );