php-wordpressHow to create a post in WordPress using PHP?
Creating a post in WordPress using PHP is a simple process. The following example code will create a post in WordPress using PHP:
$my_post = array(
'post_title' => 'My Post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1
);
// Insert the post into the database
wp_insert_post( $my_post );
This code will create a post with the title "My Post" and the content "This is my post." The post will be published and the author will be the user with the ID of 1.
The code consists of the following parts:
- An array containing the post data:
$my_post
- The
wp_insert_post()
function which inserts the post into the database
For more information, please refer to the WordPress Codex.
More of Php Wordpress
- How to echo a shortcode in WordPress using PHP?
- How to use get_header in WordPress using PHP?
- How to disable PHP warnings in WordPress?
- How to create a snippet in WordPress using PHP?
- How to create pagination in WordPress using PHP?
- How to create a WordPress plugin using PHP?
- How to set a page title in WordPress using PHP?
- How to configure Nginx for WordPress?
- How to use the WordPress REST API with PHP?
- How to add a PHP header in WordPress?
See more codes...