set_post_type

The timeline below displays how wordpress function set_post_type has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Updates 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;
}

WordPress Version: 4.3

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

WordPress Version: 4.1

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

WordPress Version: 4.0

/**
 * 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 access 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 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;
}

WordPress Version: 3.7

/**
 * Updates the post type for the post ID.
 *
 * The page or post cache will be cleaned for the post ID.
 *
 * @since 2.5.0
 *
 * @uses $wpdb
 *
 * @param int $post_id Post ID to change post type. Not actually optional.
 * @param string $post_type Optional, default is post. Supported values are 'post' or 'page' to
 *  name a few.
 * @return int 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;
}