/* ========================================================= 1. 「イベント(event)」投稿のスラッグを [日付(Ymd)+連番] に自動変更する ========================================================= */ add_filter( 'wp_insert_post_data', 'auto_set_event_slug', 10, 2 ); function auto_set_event_slug( $data, $postarr ) { // 投稿タイプが 'event' かつ、ゴミ箱行きではない場合のみ処理 if ( $data['post_type'] === 'event' && $data['post_status'] !== 'trash' ) { // すでに保存済みの投稿か、新規投稿かを確認 $post_id = ! empty( $postarr['ID'] ) ? $postarr['ID'] : 0; // 投稿の公開日(または作成日)を取得し、Ymd 形式(例: 20260723)にする $post_date = date( 'Ymd', strtotime( $data['post_date'] ) ); // 現在のスラッグが「日付+ハイフン」から始まっているかチェック(既に設定済みの場合はスキップ) if ( ! preg_match( '/^' . $post_date . '-\d{2}$/', $data['post_name'] ) ) { global $wpdb; // 同じ日付(プレフィックス)を持つスラッグの数をデータベースから検索 $slug_prefix = $post_date . '-'; // 自分自身($post_id)以外の、同じ日付プレフィックスを持つスラッグをカウント $query = $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_name LIKE %s AND post_type = 'event' AND ID != %d AND post_status != 'trash'", $wpdb->esc_like( $slug_prefix ) . '%', $post_id ); $count = $wpdb->get_var( $query ); // 連番を生成(01, 02, 03...) $sequence = str_pad( $count + 1, 2, '0', STR_PAD_LEFT ); // 新しいスラッグを生成(例: 20260723-01) $new_slug = $slug_prefix . $sequence; // 重複チェック(万が一のためにループで空き番を探す) while ( wp_unique_post_slug( $new_slug, $post_id, $data['post_status'], $data['post_type'], $data['post_parent'] ) !== $new_slug ) { $count++; $sequence = str_pad( $count + 1, 2, '0', STR_PAD_LEFT ); $new_slug = $slug_prefix . $sequence; } // データを上書き $data['post_name'] = $new_slug; } } return $data; } /* ========================================================= 2. 投稿・更新後に「公開された記事を見る」リンクを目立つように表示する ========================================================= */ add_action( 'admin_notices', 'show_custom_post_published_notice' ); function show_custom_post_published_notice() { global $post; $screen = get_current_screen(); // 現在の画面が 'event' の編集画面で、かつメッセージ(GETパラメータ)がある場合 if ( $screen && $screen->id === 'event' && isset( $_GET['message'] ) ) { // message=1 は「更新」、message=6 は「公開」を意味するWordPressの標準仕様 $msg_id = (int) $_GET['message']; if ( $msg_id === 1 || $msg_id === 6 ) { $post_url = get_permalink( $post->ID ); $message_text = ( $msg_id === 6 ) ? 'イベントを公開しました。' : 'イベントを更新しました。'; // 目立つようなカスタムメッセージボックスを出力 echo '
'; echo '

' . esc_html( $message_text ) . '

'; echo '

実際のページを確認する(別タブで開く)

'; echo '
'; } } }