Single page converted to article
//ID of the page $page_id = 123; //Set Page as Article $post = array( 'ID' => $page_id, 'post_type' => 'post', 'post_status' => 'publish' ); //Update the page using the wp_update_post function $updated_post_id = wp_update_post( $post ); //Check whether the conversion is successful if( ! is_wp_error($updated_post_id) ) { Echo "The page has been converted to an article, new article ID:". $updated_post_id; } else { Echo "Conversion failed:". $updated_post_id ->get_error_message(); }
Multiple pages converted to articles
//List of specified page IDs $page_ids_to_convert = array(123, 456, 789); // Replace with the page ID you want to convert //Traverse each page ID foreach ($page_ids_to_convert as $page_id) { //Set Page as Article $post = array( 'ID' => $page_id, 'post_type' => 'post', 'post_status'=>' publish '//or other statuses you want ); //Update the page using the wp_update_post function $updated_post_id = wp_update_post($post); //Check whether the conversion is successful if (! is_wp_error($updated_post_id)) { Echo "Page ID". $page_id. "Successfully converted to article ID". $updated_post_id. "<br>"; } else { Echo "Page ID". $page_id. "Conversion failed:". $updated_post_id ->get_error_message(). "<br>"; } }
Batch conversion of all pages to articles
//Get the ID list of all pages $args = array( 'post_type' => 'page', 'posts_per_page'=>- 1,//Retrieve all pages 'post_status'=>' any '//Retrieve all status pages ); $pages = new WP_Query($args); //Traverse each page and convert it into an article while ($pages->have_posts()) : $pages->the_post(); $page_id = get_the_ID(); //Set Page as Article $post = array( 'ID' => $page_id, 'post_type' => 'post', 'post_status'=>' publish '//or other statuses you want ); //Update the page using the wp_update_post function $updated_post_id = wp_update_post($post); //Check whether the conversion is successful if (! is_wp_error($updated_post_id)) { Echo "Page ID". $page_id. "Converted to article ID". $updated_post_id. "<br>"; } else { Echo "Page ID". $page_id. "Conversion failed:". $updated_post_id ->get_error_message(). "<br>"; } endwhile; //Reset Query wp_reset_query();
Similarly, convert articles to pages
//ID of the article $post_id = 456; //Set Article as Page $post = array( 'ID' => $post_id, 'post_type' => 'page', 'post_status'=>' publish '//or other statuses you want ); //Use the wp_update_post function to update articles $updated_post_id = wp_update_post($post); //Check whether the conversion is successful if (! is_wp_error($updated_post_id)) { Echo "The article has been converted to a page, new page ID:". $updated_post_id; } else { Echo "Conversion failed:". $updated_post_id ->get_error_message(); }