set_post_type()通过ID设置某个文章的类型

目录

描述

Updates the post type for the post ID.

The page or post cache will be cleaned for the post ID.
@uses $wpdb.

set_post_type() 描述

用法

<?php set_post_type$post_id$post_type ); ?>

set_post_type() 用法

参数

$post_id

(integer) (必填) Post ID to change.

默认值: 0

$post_type

(string) (可选) New post type name.

默认值: post

set_post_type() 参数

返回值

(integer

Amount of rows changed. Should be 1 for success and 0 for failure.

set_post_type() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?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";
}
?>

set_post_type() 示例

历史

添加于 版本: 2.5

set_post_type() 历史

源文件

set_post_type() 函数的代码位于 wp-includes/post.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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;
}

set_post_type() 源文件

相关