Create a new Page or Post Programmatically in WordPress

| Last Updated: | In: WordPress, WordPress Beginners, WP Labs


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.


About: Prem Tiwari

Prem Tiwari is the founder of freewebmentor.com and is a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby.


You may also like:


, , , , , , ,