do_all_enclosures()


WordPressのdo_all_enclosures()関数は、投稿に添付されたすべてのenclosure(メディア添付ファイル)を処理するために使用されます。RSSフィードなどで利用されるメタデータを基に動作します。

構文

do_all_enclosures( int $post_id );
  • $post_id (int) — 処理対象の投稿ID

例1: 特定の投稿のenclosureを処理

投稿IDを指定してenclosureを処理します。

do_all_enclosures(get_the_ID());

例2: 最新の投稿のenclosureを処理

最新の投稿のenclosureを自動的に処理します。

do_all_enclosures(wp_get_recent_posts(array('numberposts'=>1))[0]['ID']);

例3: カスタムクエリで取得した投稿のenclosureを処理

WP_Queryを使用して取得した投稿のenclosureを処理します。

$query=new WP_Query(array('post_type'=>'post'));while($query->have_posts()):$query->the_post();do_all_enclosures(get_the_ID());endwhile;

例4: 特定の投稿タイプのenclosureを一括処理

‘audio’投稿タイプのすべての投稿に対してenclosureを処理します。

$posts=get_posts(array('post_type'=>'audio','posts_per_page'=>-1));foreach($posts as $post){do_all_enclosures($post->ID);}

例5: スケジュールされた処理でenclosureを実行

WP Cronを使用して定期的にenclosureを処理します。

add_action('my_cron_event',function(){do_all_enclosures(get_option('target_post_id'));});wp_schedule_event(time(),'daily','my_cron_event');

例6: ショートコード内でenclosureを処理

ショートコード経由で特定の投稿のenclosureを処理します。

add_shortcode('process_enclosures',function($atts){do_all_enclosures($atts['id']);return'';});

例7: REST APIエンドポイントでenclosureを処理

カスタムREST APIエンドポイントを作成し、enclosureを処理します。

add_action('rest_api_init',function(){register_rest_route('myplugin/v1','/process-enclosures/',array('methods'=>'POST','callback'=>function($request){do_all_enclosures($request['post_id']);return rest_ensure_response(array('success'=>true));}));});

注意事項:

  • この関数は主に内部処理用に設計されており、直接呼び出す必要はほとんどありません
  • 大量の投稿を処理する場合はサーバー負荷に注意してください
  • enclosureメタデータが正しく設定されている必要があります

関連機能: