block_core_calendar_update_has_published_post_on_delete()


WordPressのblock_core_calendar_update_has_published_post_on_delete()関数は、投稿が削除された際に、カレンダーブロックの「公開済み投稿があるかどうか」の状態を更新するために使用されます。

構文

block_core_calendar_update_has_published_post_on_delete( int $post_id );
  • $post_id(int) — 削除された投稿のID。

例1: 投稿削除時にカレンダーの状態を更新

投稿が削除された際に、カレンダーブロックの状態を更新する基本的な例。

add_action( 'delete_post', 'block_core_calendar_update_has_published_post_on_delete' );

例2: 特定の投稿タイプでのみ状態を更新

特定の投稿タイプ(例: ‘post’)が削除された場合にのみ、カレンダーの状態を更新する。

function custom_update_calendar_on_delete( $post_id ) {
    if ( get_post_type( $post_id ) === 'post' ) {
        block_core_calendar_update_has_published_post_on_delete( $post_id );
    }
}
add_action( 'delete_post', 'custom_update_calendar_on_delete' );

例3: 複数の投稿タイプに対応

複数の投稿タイプ(例: ‘post’, ‘page’)が削除された場合に、カレンダーの状態を更新する。

function custom_update_calendar_on_delete_multiple( $post_id ) {
    $post_types = array( 'post', 'page' );
    if ( in_array( get_post_type( $post_id ), $post_types ) ) {
        block_core_calendar_update_has_published_post_on_delete( $post_id );
    }
}
add_action( 'delete_post', 'custom_update_calendar_on_delete_multiple' );

例4: カスタムフィールドに基づいて状態を更新

特定のカスタムフィールドが存在する場合にのみ、カレンダーの状態を更新する。

function custom_update_calendar_on_delete_custom_field( $post_id ) {
    if ( get_post_meta( $post_id, 'custom_field_key', true ) ) {
        block_core_calendar_update_has_published_post_on_delete( $post_id );
    }
}
add_action( 'delete_post', 'custom_update_calendar_on_delete_custom_field' );

例5: 特定のユーザーロールでのみ状態を更新

特定のユーザーロール(例: ‘editor’)が投稿を削除した場合にのみ、カレンダーの状態を更新する。

function custom_update_calendar_on_delete_user_role( $post_id ) {
    $user = wp_get_current_user();
    if ( in_array( 'editor', (array) $user->roles ) ) {
        block_core_calendar_update_has_published_post_on_delete( $post_id );
    }
}
add_action( 'delete_post', 'custom_update_calendar_on_delete_user_role' );

例6: 特定のカテゴリーの投稿削除時に状態を更新

特定のカテゴリー(例: ‘news’)に属する投稿が削除された場合に、カレンダーの状態を更新する。

function custom_update_calendar_on_delete_category( $post_id ) {
    if ( has_category( 'news', $post_id ) ) {
        block_core_calendar_update_has_published_post_on_delete( $post_id );
    }
}
add_action( 'delete_post', 'custom_update_calendar_on_delete_category' );

例7: 特定のタグの投稿削除時に状態を更新

特定のタグ(例: ‘important’)が付いた投稿が削除された場合に、カレンダーの状態を更新する。

function custom_update_calendar_on_delete_tag( $post_id ) {
    if ( has_tag( 'important', $post_id ) ) {
        block_core_calendar_update_has_published_post_on_delete( $post_id );
    }
}
add_action( 'delete_post', 'custom_update_calendar_on_delete_tag' );

注意点

  • この関数は、投稿が完全に削除された場合にのみ動作します。ゴミ箱に入れられた場合には動作しません。
  • カレンダーブロックが使用されている場合にのみ、この関数の効果が現れます。

関連機能: