Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Combining wp_insert_post() and update_post_meta()

2020 Update:

In case you landed here looking for a refresher on how to update the post’s meta, as I did, see the following edit:

The post array argument in wp_insert_post() takes an inner array called meta_input, so you can create the post with proper meta in one database query.

$page_id = wp_insert_post(array(
    'post_title'    =>  'Team',
    'menu_order'    =>  '2',
    'post_content'  =>  '[team]',
    'post_type'     =>  'page',
    'post_parent'   =>  1,
    'post_status'   =>  'publish',
    'meta_input'    =>  array(
        'your_meta_key' => 'your meta value'
    ),
));

The same can be done for wp_update_post():

wp_update_post(array(
  'post_title' => 'About',
  'ID' => $page_id,
  'meta_input' =>  array(
    'your_meta_key' => 'your meta value'
  ),
));

Original Answer:

is this code enough for you?

/*
** wp_insert_post - http://codex.wordpress.org/Function_Reference/wp_insert_post
** function will create a new post and return a $interger shich is a ID of the new created post.
*/
$page_id = wp_insert_post(array(
    'post_title'    =>  'Team',
    'menu_order'    =>  '2',
    'post_content'  =>  '[team]',
    'post_type'     =>  'page',
    'post_parent'   =>  1,
    'post_status'   =>  'publish',
));

/*
**  wp_update_post - http://codex.wordpress.org/Function_Reference/wp_update_post
** function is simular (actually its almost alias) of wp_insert_post
** it will update a post 
** $arguments that passed to wp_update_post is a same as in wp_insert_post but require additional argument ID whish is id of the post
*/
wp_update_post(array('post_title' => 'About', 'ID' => $page_id));

Related Posts:

  1. wp_insert_post() getting slower the more posts
  2. Efficient way to save a lot of meta data
  3. How to use wp_insert_post to update meta box?
  4. wp_insert_post bulk import 500 server error
  5. update_post_meta, xml parser
  6. Get original post ID after wp_insert_post
  7. Update post_meta post_name with the post ID from wp_insert_post after user register
  8. Insert post programmatically and decide to tweet or not to tweet
  9. How to update custom fields using the wp_insert_post() function?
  10. Attaching taxonomy data to post with wp_insert_post
  11. Faster way to wp_insert_post & add_post_meta in bulk
  12. wordpress sanitize array?
  13. Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
  14. Is there a downside of using wp_defer_term_counting?
  15. wp_insert_post extremely slow on big table, direct query very fast
  16. What is “meta_input” parameter in wp_insert_post() used for?
  17. wp_insert_post add meta_input
  18. tax_input not working wp_insert_post
  19. wp set object terms vs wp set post terms
  20. See error message from wp_insert_post function?
  21. Adding post thumbnail in programatically inserted post
  22. can’t edit post_modified in wp_insert_post (bug?)
  23. advanced custom fields update_field for field type: Taxonomy
  24. wp_insert_post incorrectly escapes HTML comments when they include tags
  25. What is considered the post’s creation date for wp_insert_post?
  26. Insert new term during new post creation
  27. wp_insert_post creates multiple pages
  28. How to allow data:image attribute in src tag during post insert?
  29. tax_input argument for wp_insert_post()
  30. How can I stop wp_update_post messing up HTML example code?
  31. XMLRPC Avoid duplicate content
  32. How to insert custom function into wp_insert_post
  33. wp_insert_post does not write my post_name
  34. Is there a way to fire other functions from wp_insert_post
  35. Programmatically add terms to custom post types
  36. Organizing uploaded media with wp_insert_post() and wp_handle_upload according to time parameter
  37. wp_insert_post not returning post ID? [closed]
  38. wp_insert_post to schedule a post – but nothing happens?
  39. post_date_gmt and post_date [duplicate]
  40. wp_insert_post() Callback?
  41. Get data from wp_editor()
  42. wp_insert post doesn’t work
  43. Replace string with post_name on sidebar
  44. Inserting Hundreds of Thousands of Posts at Once
  45. Visual Editor – Colorize Shortcodes
  46. wp_insert_post insert a post but return 0
  47. How to update post parent?
  48. wp_insert_post returns int(0)
  49. Contact Form 7 to featured image
  50. Conditionally set post_content in wp_insert_post
  51. wp_insert_post -> post_content not showing
  52. after wp_insert_post need to get the post id
  53. Is wp_insert_post exactly the same as publishing a post through the core UI?
  54. Could not get post ID from wp_insert_post()
  55. Creating New Page with WP_INSERT_POST Issue
  56. wp_insert_post is not creating revision in database
  57. Set default Custom Post Meta Value
  58. wp_insert_post issue
  59. Adding custom fields (post meta) before/during wp_insert_post()
  60. Inserting post, thumnail and custom fields with wp_insert_post
  61. add_menu_page Callback Function: Skip page content?
  62. update post meta for checkbox in the admin when inserted in the front-end
  63. WPML – Set language of inserted post
  64. Can’t get post ID using wp_insert_post_data
  65. using wp_insert_post inside of custom meta box
  66. wp_insert_post or wp_set_post_terms do not save taxonomy, but wp_set_post_terms does
  67. Making sure wp term relationships records are unique
  68. wp_insert_post if page doesn’t exist under current page
  69. Check if post is added manually or through wp_insert_post()
  70. How to upload multiple image using wp_insert_post
  71. When User Meta amended update Title and Slug of Post programatically
  72. Compare old meta with new post meta
  73. Attaching taxonomy data to taxonomy with wp_insert_post
  74. keep wp_insert_post from adding duplicates
  75. Update post meta does not serialize array
  76. Add file while inserting post
  77. Programmatically create a post once a day
  78. wp_insert_post custom type and custom taxonomies
  79. Where to call wp_insert_user() and wp_insert_post() from?
  80. wp_insert_post at same time as saving post results in 502 Bad Gateway
  81. Add action to create new post after wp_insert_post fires? Think it’s creating an infitnite loop
  82. wp_insert_post alternatives for faster process
  83. WP-CLI –meta-input error: Warning: Invalid argument supplied for foreach()
  84. tax_input in wp_insert_post partly not working
  85. Issue with wp_insert_post() when post_content contains links?
  86. $user_id = wp_insert_user( $userdata ) Not working no errors
  87. $wpdb->insert() does not insert fields
  88. How to handle this specific case of custom post type?
  89. Insert multiple posts as parent of the first
  90. oEmbded with wp_insert_post() [embedly]
  91. Insert data from custom created PHP page into wp_postmeta table
  92. wp_insert_post Only creates draft
  93. Save the Post ID from wp_insert_post($post); into a text file?
  94. wp_insert_post removes information when updating
  95. Problem with wp_insert_post
  96. Bug in wp_insert_post script and form, probably something simple I am not seeing
  97. Custom Post Type not showing anything added by code anymore?
  98. How can I get some of a posts meta data whilst it is still inserting?
  99. Setting Post Date Returning “Notice: A non well formed numeric value encountered”
  100. Custom terms inserting as single string with commas, not separated
Categories wp-insert-post Tags post-meta, wp-insert-post
Conditional tags not working
using wp_tag_cloud with custom taxonomy

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress