wp_cache_set_comments_last_changed()


WordPressのwp_cache_set_comments_last_changed()関数は、コメントの最終変更タイムスタンプをキャッシュに保存するために使用されます。この関数は、コメントデータのキャッシュを管理する際に役立ちます。

構文

wp_cache_set_comments_last_changed( $group = 'comment' );
  • $group(string)—キャッシュグループ名。デフォルトは’comment’。

例1:コメントの最終変更タイムスタンプを更新

コメントが追加された後にキャッシュを更新します。

wp_cache_set_comments_last_changed();

例2:カスタムキャッシュグループを使用

カスタムキャッシュグループを指定してタイムスタンプを更新します。

wp_cache_set_comments_last_changed('custom_comments');

例3:コメントクエリ前にキャッシュをリセット

コメントを取得する前にキャッシュをリセットします。

wp_cache_set_comments_last_changed();$comments=get_comments();

例4:プラグイン内での使用

プラグイン内でコメントキャッシュを管理します。

add_action('comment_post','my_update_comment_cache');function my_update_comment_cache(){wp_cache_set_comments_last_changed();}

例5:特定の投稿のコメントキャッシュを更新

特定の投稿IDに関連するコメントキャッシュを更新します。

wp_cache_set_comments_last_changed();clean_post_cache(get_the_ID());

例6:マルチサイト環境での使用

マルチサイト環境でコメントキャッシュを更新します。

switch_to_blog(2);wp_cache_set_comments_last_changed();restore_current_blog();

例7:コメント削除時のキャッシュ更新

コメントが削除された際にキャッシュを更新します。

add_action('delete_comment','my_comment_deleted_action');function my_comment_deleted_action(){wp_cache_set_comments_last_changed();}

注意点

  • この関数はオブジェクトキャッシュが有効な場合のみ効果があります。
  • 直接呼び出すよりも、適切なフックで使用することを推奨します。

関連機能: