set_post_type() WordPress Function

The set_post_type() function is used to set the post type for a post.

set_post_type( int $post_id, string $post_type = 'post' ) #

Update the post type for the post ID.


Description

The page or post cache will be cleaned for the post ID.


Top ↑

Parameters

$post_id

(int)(Optional) Post ID to change post type. Default 0.

$post_type

(string)(Optional) Post type. Accepts 'post' or 'page' to name a few.

Default value: 'post'


Top ↑

Return

(int|false) Amount of rows changed. Should be 1 for success and 0 for failure.


Top ↑

Source

File: wp-includes/post.php

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


Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More