set_post_type

Update the post type for the post ID.

Description

(int|false) set_post_type( (int) $post_id = 0, (string) $post_type = 'post' ); 

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

Returns (int|false)

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

Parameters (2)

0. $post_id — Optional. (int)
Post ID to change post type. Default 0.
1. $post_type — Optional. (string) => 'post'
Post type. Accepts post or page. to name a few. Default post .

Usage

  1. if ( !function_exists( 'set_post_type' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. Post ID to change post type. Default 0. 
  5. $post_id = -1; 
  6.  
  7. // Optional. Post type. Accepts 'post' or 'page' to 
  8. // name a few. Default 'post'. 
  9. $post_type = 'post'; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = set_post_type($post_id, $post_type); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function set_post_type( $post_id = 0, $post_type = 'post' ) { 
  2. global $wpdb; 
  3.  
  4. $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db'); 
  5. $return = $wpdb->update( $wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) ); 
  6.  
  7. clean_post_cache( $post_id ); 
  8.  
  9. return $return;