set_post_type

函式
set_post_type ( $post_id = 0, $post_type = 'post' )
引數
  • (int) $post_id Optional. Post ID to change post type. Default 0.
    Required:
  • (string) $post_type Optional. Post type. Accepts 'post' or 'page' to name a few. Default 'post'.
    Required:
    Default: 'post'
返回值
  • (int|false) Amount of rows changed. Should be 1 for success and 0 for failure.
定義位置
相關方法
get_post_typeget_post_typesregister_post_typeget_post_mime_typeget_post_time
引入
2.5.0
棄用
-

set_post_type: 這是一個WordPress的函式,為當前的文章設定文章型別。它通常被用來以程式設計方式改變一個文章的文章型別: 這個函式需要兩個引數:文章的ID和新的文章型別。

更新文章ID的文章型別。

頁面或文章的快取將為文章ID進行淨化。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >