WordPress article and page exchange

 Robin
Robin
stationmaster
two thousand four hundred and nineteen
article
zero
fans
WordPress four five hundred and sixty-six Reading mode

Sometimes you may be ready to convert one or more pages into articles. Please refer to the following methods.

Add code to the current topic function template functions.php:

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

If it is troublesome to use the code, you can consider using the WordPress article type conversion plug-in: Post Type Switcher

WordPress文章与页面互换
WordPress itself cannot directly convert the page to a normal article. There is no alternative but to copy the content and republish it
seven hundred and twenty-four

This plug-in can not only convert pages and articles to each other, but also convert custom article types.

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
WeChat copied
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments four    Visitors four
    •  Xiaoxiao
      Xiaoxiao one

      Technical Tutorial

      •  Qipai space
        Qipai space one

        Thank you for sharing practical tutorials.

        •  one thousand nine hundred and twenty
          one thousand nine hundred and twenty one

          This is useful. Try it.

          •  Zhu Xu
            Zhu Xu zero

            This is very convenient

           anonymous

          Comment

          Anonymous netizens

          Drag the slider to complete validation