Create a new Page or Post Programmatically in WordPress

Prem Tiwari - - WordPress, WordPress Beginners, WordPress Snippets


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.


Prem Tiwari

Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer and a blogger by hobby.

Tags: , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.