In creating a plugin, I ran across the need to add a WordPress post or page programmatically. This is very easy if you are developing or have developed a theme or plugin. You will need to use the wp_insert_post WordPress function. See the code below to add a WordPress post or a WordPress page programmatically. A full breakdown of the parameters available is visible at https://developer.wordpress.org/reference/functions/wp_insert_post/.
Create WordPress Post Programmatically
You will need to add this code to a function that will run in a WordPress plugin or a WordPress theme, such as a functions.php file.
$wordpress_post = array(
'post_title' => 'Post title',
'post_content' => 'Post Content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'post'
);
wp_insert_post( $wordpress_post );
Create WordPress Page Programmatically
Again, you will need to insert this code into a function that will run in a WordPress plugin or a WordPress theme, such as a functions.php file. The big difference between this and the code above is the post_type value, which is page below and post above.
$wordpress_page = array(
'post_title' => 'Page title',
'post_content' => 'Page Content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
wp_insert_post( $wordpress_page );