wp_trash_post

函式
wp_trash_post ( $post_id = 0 )
引數
  • (int) $post_id Optional. Post ID. Default is the ID of the global `$post` if `EMPTY_TRASH_DAYS` equals true.
    Required:
返回值
  • (WP_Post|false|null) Post data on success, false or null on failure.
相關
  • wp_delete_post()
定義位置
相關方法
wp_untrash_postwp_ajax_trash_postwp_ajax_untrash_postwp_trash_commentwp_trash_post_comments
引入
2.9.0
棄用
-

wp_trash_post: 這個函式用來扔掉一個文章(移動到回收站): 當一個文章被扔掉時,它的狀態會被改變為”垃圾”,並且不會在網站的前端顯示: 這個函式可以通過傳遞文章的ID作為引數來使用。

將一個文章或頁面移到回收站。

如果回收站被禁用,該文章或頁面將被永久刪除。

function wp_trash_post( $post_id = 0 ) {
	if ( ! EMPTY_TRASH_DAYS ) {
		return wp_delete_post( $post_id, true );
	}

	$post = get_post( $post_id );

	if ( ! $post ) {
		return $post;
	}

	if ( 'trash' === $post->post_status ) {
		return false;
	}

	/**
	 * Filters whether a post trashing should take place.
	 *
	 * @since 4.9.0
	 *
	 * @param bool|null $trash Whether to go forward with trashing.
	 * @param WP_Post   $post  Post object.
	 */
	$check = apply_filters( 'pre_trash_post', null, $post );

	if ( null !== $check ) {
		return $check;
	}

	/**
	 * Fires before a post is sent to the Trash.
	 *
	 * @since 3.3.0
	 *
	 * @param int $post_id Post ID.
	 */
	do_action( 'wp_trash_post', $post_id );

	add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status );
	add_post_meta( $post_id, '_wp_trash_meta_time', time() );

	$post_updated = wp_update_post(
		array(
			'ID'          => $post_id,
			'post_status' => 'trash',
		)
	);

	if ( ! $post_updated ) {
		return false;
	}

	wp_trash_post_comments( $post_id );

	/**
	 * Fires after a post is sent to the Trash.
	 *
	 * @since 2.9.0
	 *
	 * @param int $post_id Post ID.
	 */
	do_action( 'trashed_post', $post_id );

	return $post;
}

常見問題

FAQs
檢視更多 >