How to programmatically create posts in wordpress?

admin2025-06-03  3

I have a very intensive data backed WordPress application. Currently I create WordPress posts by using a baseshortcode plugin which pulls data from database and displays in tabular format.

I would like to automate this process where in the posts are created automatically in draft state.

I have a very intensive data backed WordPress application. Currently I create WordPress posts by using a baseshortcode plugin which pulls data from database and displays in tabular format.

I would like to automate this process where in the posts are created automatically in draft state.

Share Improve this question edited Feb 1, 2019 at 14:17 skillguru asked Jun 25, 2014 at 13:43 skillguruskillguru 1471 silver badge7 bronze badges 3
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – engelen Commented Jun 25, 2014 at 13:50
  • 2 So you're asking "How do I programmatically create a new post?" – Tom J Nowell Commented Jun 25, 2014 at 13:56
  • I've removed the request for a plugin to do this as plugin recommendations are off topic here – Tom J Nowell Commented Jun 25, 2014 at 13:57
Add a comment  | 

1 Answer 1

Reset to default 3

You can use the wp_insert_post() function to accomplish creating a new post, including it being in a draft state. As you can see in this snippet from the Codex, you can control all the aspects of the post, including post date, draft status, post type, categories, and much more.

  $post = array(
  'ID'             => [ <post id> ] // Are you updating an existing post?
  'post_content'   => [ <string> ] // The full text of the post.
  'post_name'      => [ <string> ] // The name (slug) for your post
  'post_title'     => [ <string> ] // The title of your post.
  'post_status'    => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] // Default 'draft'.
  'post_type'      => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] // Default 'post'.
  'post_author'    => [ <user ID> ] // The user ID number of the author. Default is the current user ID.
  'ping_status'    => [ 'closed' | 'open' ] // Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'.
  'post_parent'    => [ <post ID> ] // Sets the parent of the new post, if any. Default 0.
  'menu_order'     => [ <order> ] // If new post is a page, sets the order in which it should appear in supported menus. Default 0.
  'to_ping'        => // Space or carriage return-separated list of URLs to ping. Default empty string.
  'pinged'         => // Space or carriage return-separated list of URLs that have been pinged. Default empty string.
  'post_password'  => [ <string> ] // Password for post, if any. Default empty string.
  'guid'           => // Skip this and let Wordpress handle it, usually.
  'post_content_filtered' => // Skip this and let Wordpress handle it, usually.
  'post_excerpt'   => [ <string> ] // For all your post excerpt needs.
  'post_date'      => [ Y-m-d H:i:s ] // The time post was made.
  'post_date_gmt'  => [ Y-m-d H:i:s ] // The time post was made, in GMT.
  'comment_status' => [ 'closed' | 'open' ] // Default is the option 'default_comment_status', or 'closed'.
  'post_category'  => [ array(<category id>, ...) ] // Default empty.
  'tags_input'     => [ '<tag>, <tag>, ...' | array ] // Default empty.
  'tax_input'      => [ array( <taxonomy> => <array | string> ) ] // For custom taxonomies. Default empty.
  'page_template'  => [ <string> ] // Requires name of template file, eg template.php. Default empty.
);  

  wp_insert_post($new_post);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748953591a315117.html

最新回复(0)