yuheijotaki.com

2018/09/27 : 
WordPress 投稿ステータスの変更を取得して処理する

/** 投稿状態変更のタイミングでカスタムフィールド変更 **/
/**
 * @param string $new_status 新しいステータス
 * @param string $old_status 以前のステータス
 * @param object $post 該当する投稿オブジェクト
 */
function update_custom_date_when_published($new_status, $old_status, $post){
	// 投稿のステータス 更新前->ドラフト、 更新後->公開済み の場合のみ
	if ( $old_status == 'draft' && $new_status == 'publish' ) {
		// 無限ループを防ぐために機能を削除
		remove_action('transition_post_status', 'update_custom_date_when_published');
		wp_update_post($post);
		date_default_timezone_set('Asia/Tokyo'); // タイムゾーンを設定
		$post_id = $post->ID; // この投稿IDを取得
		$custom_date = get_the_date( 'Y/m/d H:i:s', $post_id ); // get_the_date()でこの投稿の日時を取得
		$custom_date = strtotime( $custom_date ); // UNIX TIMESTAMP として取得
		update_field('custom_date', $custom_date, $post_id); // カスタムフィールドに日付を追加
	}
	// 投稿のステータス 予約投稿、 更新後->公開済み の場合
	if ( $old_status == 'future' && $new_status == 'publish' ) {
		// ここに内容
	}
}
add_action('transition_post_status', 'update_custom_date_when_published', 10, 3);