wp_trash_post()


WordPressのwp_trash_post()関数は、指定された投稿をゴミ箱に移動するために使用されます。この関数は、投稿を完全に削除するのではなく、一時的に非表示にします。

構文

wp_trash_post( int $post_id );
  • $post_id(int) — ゴミ箱に移動する投稿のID。

例1: 特定の投稿をゴミ箱に移動する

この例では、IDが123の投稿をゴミ箱に移動します。

wp_trash_post(123);

例2: 現在の投稿をゴミ箱に移動する

この例では、現在表示されている投稿をゴミ箱に移動します。

wp_trash_post(get_the_ID());

例3: 最新の投稿をゴミ箱に移動する

この例では、最新の投稿をゴミ箱に移動します。

$latest_post = get_posts('numberposts=1'); wp_trash_post($latest_post[0]->ID);

例4: 特定のカテゴリの投稿をゴミ箱に移動する

この例では、カテゴリIDが5の投稿をゴミ箱に移動します。

$posts = get_posts(array('category' => 5)); foreach ($posts as $post) { wp_trash_post($post->ID); }

例5: 特定の著者の投稿をゴミ箱に移動する

この例では、著者IDが2の投稿をゴミ箱に移動します。

$posts = get_posts(array('author' => 2)); foreach ($posts as $post) { wp_trash_post($post->ID); }

例6: 特定の日付以前の投稿をゴミ箱に移動する

この例では、2022年1月1日以前の投稿をゴミ箱に移動します。

$posts = get_posts(array('date_query' => array(array('before' => '2022-01-01')))); foreach ($posts as $post) { wp_trash_post($post->ID); }

例7: 特定のステータスの投稿をゴミ箱に移動する

この例では、下書き状態の投稿をゴミ箱に移動します。

$posts = get_posts(array('post_status' => 'draft')); foreach ($posts as $post) { wp_trash_post($post->ID); }

例8: 複数の投稿を一度にゴミ箱に移動する

この例では、複数の投稿IDを指定して一度にゴミ箱に移動します。

$post_ids = array(123, 456, 789); foreach ($post_ids as $post_id) { wp_trash_post($post_id); }

注意点

  • ゴミ箱に移動した投稿は、完全に削除されるまでデータベースに残ります。
  • ゴミ箱から投稿を復元するにはwp_untrash_post()関数を使用します。
  • 投稿を完全に削除するにはwp_delete_post()関数を使用します。

関連機能: