描述:
通过文章ID更新自定义文章类型的设置
用法:
<?php set_post_type( $post_id, $post_type ); ?>
参数:
$post_id
(integer) (必填) 文章ID
默认值: 0
$post_type
(string) (可选) 新的自定义文章类型名称
默认值: post
示例:
<?php
$post_id = 15;
if ( set_post_type( $post_id, 'page' ) ) {
echo "Post #$post_id is now a Page";
} else {
echo "Impossible to transform this post into a page";
}
?>
源文件:
/**
* Update the post type for the post ID.
*
* The page or post cache will be cleaned for the post ID.
*
* @since 2.5.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $post_id Optional. Post ID to change post type. Default 0.
* @param string $post_type Optional. Post type. Accepts 'post' or 'page' to
* name a few. Default 'post'.
* @return int|false Amount of rows changed. Should be 1 for success and 0 for failure.
*/
function set_post_type( $post_id = 0, $post_type = 'post' ) {
global $wpdb;
$post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
$return = $wpdb->update( $wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) );
clean_post_cache( $post_id );
return $return;
}