Title: set_post_type
Published: April 25, 2014
Last modified: February 24, 2026

---

# set_post_type( int $post_id, string $post_type ): int|false

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#wp--skip-link--target)

Updates the post type for the post ID.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#description)󠁿

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#parameters)󠁿

 `$post_id`intoptional

Post ID to change post type. Default 0.

`$post_type`stringoptional

Post type. Accepts `'post'` or `'page'` to name a few. Default `'post'`.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post.php#L2388)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post.php#L2388-L2397)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#related)󠁿

| Uses | Description | 
| [clean_post_cache()](https://developer.wordpress.org/reference/functions/clean_post_cache/)`wp-includes/post.php` |

Will clean the post in the cache.

  | 
| [sanitize_post_field()](https://developer.wordpress.org/reference/functions/sanitize_post_field/)`wp-includes/post.php` |

Sanitizes a post field based on context.

  | 
| [wpdb::update()](https://developer.wordpress.org/reference/classes/wpdb/update/)`wp-includes/class-wpdb.php` |

Updates a row in the table.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#comment-content-1339)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/set_post_type/#comment-1339)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_post_type%2F%23comment-1339)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_post_type%2F%23comment-1339)
 4.  **Example**
 5.      ```php
         $post_id = 15;
     
         if ( set_post_type( $post_id, 'page'  ) ) {
           printf( __( 'Post #%d is now a Page', 'textdomain' ), $post_id );
         } else {
           echo __( 'Impossible to transform this post into a page.', 'textdomain' );
         }
         ```
     
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_post_type%2F%3Freplytocom%3D1339%23feedback-editor-1339)
 7.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/set_post_type/?output_format=md#comment-content-6567)
 8.    [mayur8991](https://profiles.wordpress.org/mayur8991/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/set_post_type/#comment-6567)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_post_type%2F%23comment-6567)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_post_type%2F%23comment-6567)
 10.  * Move or assign any post into specific post type
      * After successfully assign to post type post data will remain same in the post
        like post title, post content
      * Works with custom post types
 11.     ```php
         $post_id = 123; //your post id
         $post_type = 'page'; //your post type name also you can use custom post type
     
         if ( set_post_type( $post_id, $post_type  ) ) {
           printf( __( 'Post %d is now a %s', 'textdomain' ), $post_id, $post_type );
         } else {
            printf( __( 'Impossible to transform this post into a %s', 'textdomain' ), $post_type );
         }
         ```
     
 12.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_post_type%2F%3Freplytocom%3D6567%23feedback-editor-6567)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fset_post_type%2F)
before being able to contribute a note or feedback.