Create a new Page or Post Programmatically in WordPress

If you want to create some demo pages OR Posts during your custom wordpress theme installation, then you can use the wp_insert_post() function inside your functions.php file.

Open functions.php file of your theme’s folder and copy paste below code at the end of the file. Below code will create a new page when code will execute. And of course It will be work for POST and other custom post type. You need to change 'post_type' => 'post'.

wordpress-beginner-guide

wp_insert_post function will returns the ID of the newly-created page or post in $new_page_id variable. If it will occure an error, it will return 0 in $new_page_id variable.

Do you like & share this article with your friends, and don’t forget to follow us on Facebook and Twitter to learn cool WordPress tutorials.

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

Recommended Posts:


Prem Tiwari

An engineer by profession and a passionate blogger by heart. Founder of FreeWebMentor.com (A Programming blog for beginners), Tech Speaker at various forums. A part from this he is an open source enthusiast, WordPress Lover, Blogger, SEO, and Growth Strategic.

Article Tags: , , , , , , ,