{"id":81039,"date":"2023-03-17T10:42:30","date_gmt":"2023-03-17T10:42:30","guid":{"rendered":"\/tutorials\/?p=81039"},"modified":"2026-03-09T19:18:06","modified_gmt":"2026-03-09T19:18:06","slug":"how-to-use-wp-update-post-in-wordpress","status":"publish","type":"post","link":"\/ng\/tutorials\/how-to-use-wp-update-post-in-wordpress","title":{"rendered":"How to use wp_update_post to update WordPress posts"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Mega-WordPress-Cheat-EN.pdf\">Download all-in-one WordPress cheat sheet<\/a><\/p><p>Updating posts via the WordPress dashboard is a simple and easy task.<\/p><p>However, you can also test out more advanced WordPress features or learn how WordPress functions work during the process. To accomplish this, consider updating posts with the <strong>wp_update_post() <\/strong>function.<\/p><p>In this tutorial, we will explain the WordPress <strong>wp_update_post()<\/strong> function and cover its most popular use cases.<\/p><p>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-parameters-of-the-wp-update-post-wordpress-function\">Parameters of the wp_update_post WordPress Function<\/h2><p>The<strong> wp_update_post() <\/strong>function updates existing posts in the database. Check out its parameters below:<\/p><pre class=\"wp-block-preformatted\">wp_update_post( $postarr, $wp_error, $fire_after_hooks );<\/pre><ul class=\"wp-block-list\">\n<li><strong>$postarr <\/strong>&ndash; the post data array.<\/li>\n\n\n\n<li><strong>$wp_error<\/strong> &ndash; determines whether to return a <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wp_error\/\">WP_Error<\/a> class in case of an error. Set as <strong>false<\/strong> by default.<\/li>\n\n\n\n<li><strong>$fire_after_hooks<\/strong> &ndash; decides whether to fire the after-insert hooks. By default, it is set as <strong>true<\/strong>.<\/li>\n<\/ul><p>As for return values, the function will return the post ID if successful. Otherwise, you will see <strong>WP_Error<\/strong> or value zero.<\/p><p>In other words,<strong> wp_update_post()<\/strong> takes an array of post data as an argument and updates the corresponding posts in the database with the new post data.<\/p><p>Check the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_update_post\/#source\">function source<\/a> from the <strong>wp-includes\/post.php<\/strong> file below:<\/p><p>function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hooks = true ) {<\/p><pre class=\"wp-block-preformatted\">function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hooks = true ) {\n\tif ( is_object( $postarr ) ) {\n\t\t\/\/ Non-escaped post was passed.\n\t\t$postarr = get_object_vars( $postarr );\n\t\t$postarr = wp_slash( $postarr );\n\t}\n\n\t\/\/ First, get all of the original post fields.\n\t$post = get_post( $postarr['ID'], ARRAY_A );\n\n\tif ( is_null( $post ) ) {\n\t\tif ( $wp_error ) {\n\t\t\treturn new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );\n\t\t}\n\t\treturn 0;\n\t}\n\n\t\/\/ Escape data pulled from DB.\n\t$post = wp_slash( $post );\n\n\t\/\/ Passed post category list overwrites existing category list if not empty.\n\tif ( isset( $postarr['post_category'] ) &amp;&amp; is_array( $postarr['post_category'] )\n\t\t&amp;&amp; count( $postarr['post_category'] ) &gt; 0\n\t) {\n\t\t$post_cats = $postarr['post_category'];\n\t} else {\n\t\t$post_cats = $post['post_category'];\n\t}\n\n\t\/\/ Drafts shouldn't be assigned a date unless explicitly done so by the user.\n\tif ( isset( $post['post_status'] )\n\t\t&amp;&amp; in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true )\n\t\t&amp;&amp; empty( $postarr['edit_date'] ) &amp;&amp; ( '0000-00-00 00:00:00' === $post['post_date_gmt'] )\n\t) {\n\t\t$clear_date = true;\n\t} else {\n\t\t$clear_date = false;\n\t}\n\n\t\/\/ Merge old and new fields with new fields overwriting old ones.\n\t$postarr                  = array_merge( $post, $postarr );\n\t$postarr['post_category'] = $post_cats;\n\tif ( $clear_date ) {\n\t\t$postarr['post_date']     = current_time( 'mysql' );\n\t\t$postarr['post_date_gmt'] = '';\n\t}\n\n\tif ( 'attachment' === $postarr['post_type'] ) {\n\t\treturn wp_insert_attachment( $postarr, false, 0, $wp_error );\n\t}\n\n\t\/\/ Discard 'tags_input' parameter if it's the same as existing post tags.\n\tif ( isset( $postarr['tags_input'] ) &amp;&amp; is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) {\n\t\t$tags      = get_the_terms( $postarr['ID'], 'post_tag' );\n\t\t$tag_names = array();\n\n\t\tif ( $tags &amp;&amp; ! is_wp_error( $tags ) ) {\n\t\t\t$tag_names = wp_list_pluck( $tags, 'name' );\n\t\t}\n\n\t\tif ( $postarr['tags_input'] === $tag_names ) {\n\t\t\tunset( $postarr['tags_input'] );\n\t\t}\n\t}\n\n\treturn wp_insert_post( $postarr, $wp_error, $fire_after_hooks );\n}<\/pre><p><div class=\"protip\">\n                    <h4 class=\"title\">Suggested Reading<\/h4>\n                    <p><a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_update_post\/#user-contributed-notes\">Official wp_update_post WordPress Documentation Page<\/a><br>\n<a href=\"\/ng\/tutorials\/wp-insert-post\">How to Use the wp_insert_post Function<\/a><br>\n<a href=\"\/ng\/tutorials\/wordpress-get-post-meta\">How to Use WordPress get_post_meta Function<\/a><\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-use-the-wp-update-post-function-in-wordpress\">How to Use the wp_update_post Function in WordPress<\/h2><p>To use the <strong>wp_update_post()<\/strong> function, you need to create a default array first, which will be passed as a parameter. Then, fill in the function with the fields you want to change, namely:<\/p><ul class=\"wp-block-list\">\n<li><strong>ID<\/strong> &ndash; a unique number assigned to each post.<\/li>\n\n\n\n<li><strong>post_author<\/strong> &ndash; a post author&rsquo;s user ID.<\/li>\n\n\n\n<li><strong>post_content<\/strong> &ndash; post content, including text and images.<\/li>\n\n\n\n<li><strong>post_date<\/strong> &ndash; the date and time when the post was published.<\/li>\n\n\n\n<li><strong>post_date_gmt<\/strong> &ndash; the GMT time zone in which the post date is written.<\/li>\n\n\n\n<li><strong>post_excerpt<\/strong> &ndash; posts&rsquo; user-defined excerpt.<\/li>\n\n\n\n<li><strong>post_title<\/strong> &ndash; a full post title.<\/li>\n\n\n\n<li><strong>post_status<\/strong> &ndash; a current post status, such as published, draft, or trash.<\/li>\n\n\n\n<li><strong>comment_status<\/strong> &ndash; enable or disable comments for a given post.<\/li>\n\n\n\n<li><strong>post_type<\/strong> &ndash; a default post type, such as a page or an attachment.<\/li>\n\n\n\n<li><strong>post_modified<\/strong> &ndash; posts&rsquo; last modified date and time.<\/li>\n\n\n\n<li><strong>post_modified_gmt<\/strong> &ndash; the GMT time zone in which the post was modified.<\/li>\n\n\n\n<li><strong>ping_status<\/strong> &ndash; open or close a post for pingbacks.<\/li>\n\n\n\n<li><strong>post_password<\/strong> &ndash; a post password. It will be empty if there is no password.<\/li>\n\n\n\n<li><strong>post_parent<\/strong> &ndash; a parent post ID if it exists.<\/li>\n\n\n\n<li><strong>comment_count<\/strong> &ndash; the number of comments on a post.<\/li>\n<\/ul><p><div class=\"protip\">\n                    <h4 class=\"title\">Pro Tip<\/h4>\n                    <p>The <strong>wp_update_post()<\/strong> function can cause an infinite loop. It happens when <strong>post_type<\/strong> is set as <strong>revision<\/strong> and <strong>wp_update_post()<\/strong> is used within the <strong>save_post<\/strong> hook. To prevent this, make sure that <strong>post_type<\/strong> is not set as revision.<\/p>\n                <\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-wordpress-wp-update-post-function-examples\">WordPress wp_update_post Function Examples<\/h2><p>Check out a few popular use cases of the <strong>wp_update_post()<\/strong> function.<\/p><p><strong>Update Post Meta<\/strong><\/p><p>With <strong>wp_update_post()<\/strong>, you can also update your WordPress posts metadata to improve your SEO efforts. Just use the code below:<\/p><pre class=\"wp-block-preformatted\">$mydata = array(\n  'ID' =&gt; $post_id,\n  'post_content' =&gt; $content,\n  'meta_input' =&gt; array(\n  'meta_key' =&gt; $meta_value,\n  'next_meta_key' =&gt; $next_meta_value\n   )\n );\n\nwp_update_post( $data );<\/pre><p><strong>Update Post<\/strong><\/p><p>It&rsquo;s possible to update a post more easily with <strong>wp_update_post()<\/strong>. All you need to do is pass the post ID along with the elements you want to update. Keep in mind that element names must match those in your database.<\/p><pre class=\"wp-block-preformatted\">$my_awesome_post = array(\n      'ID'           =&gt; 10,\n      'post_title'   =&gt; 'This is my awesome post title',\n      'post_content' =&gt; 'This is my awesome updated content.',\n  );\n\n\/\/ Update the specified post into the database\n  wp_update_post( $my_post );\nProcessing $wp_error<\/pre><p>We recommend enabling an error display with the following code if the update doesn&rsquo;t work.<\/p><pre class=\"wp-block-preformatted\">&lt;?php\nwp_update_post( $current_item, true );\t\t\t\t\t\t  \nif (is_wp_error($post_id)) {\n\t$errors = $post_id-&gt;get_error_messages();\n\tforeach ($errors as $error) {\n\t\techo $error;\n\t}\n}\n?&gt;<\/pre><p>Make sure to disable the error display again when publishing the changes to production.<\/p><p><strong>Automatically Publish a Post In The Future<\/strong><\/p><p>You can set a post to be published in the future. For example, the following code sample will schedule a post to be published tomorrow:<\/p><pre class=\"wp-block-preformatted\">$time = strtotime( 'tomorrow' );\n$my_post = array(\n    'ID'            =&gt; 1,\n    'post_status'   =&gt; 'future',\n    'post_date'     =&gt; date( 'Y-m-d H:i:s', $time ),\n    'post_date_gmt' =&gt; gmdate( 'Y-m-d H:i:s', $time ),\n);\nwp_update_post( $my_post );<\/pre><?xml encoding=\"utf-8\" ?><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-wordpress-hosting\" href=\"\/ng\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"600\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/public\" alt=\"\" class=\"wp-image-111781\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=1536,fit=scale-down 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>Updating your posts via a function is a great way to learn more about the advanced WordPress features.<\/p><p>In this tutorial, we went over the <strong>wp_update_post()<\/strong> function, shown how it works, and provided a few helpful use cases.<\/p><p>We hope that now you understand the <strong>wp_update_post() <\/strong>function better and will be able to use it successfully with your WordPress projects.<br>Should you have any questions, check out our <a href=\"\/ng\/tutorials\/wordpress\">WordPress guide<\/a> or leave a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Updating posts via the WordPress dashboard is a simple and easy task. However, you can also test out more advanced WordPress features or learn how WordPress functions work during the process. To accomplish this, consider updating posts with the wp_update_post() function. In this tutorial, we will explain the WordPress wp_update_post() function and cover its most [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ng\/tutorials\/how-to-use-wp-update-post-in-wordpress\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":279,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"wp_update_post: How to Update WordPress Posts","rank_math_description":"The wp_update_post function updates WordPress posts without changing the modified date. Check this article to learn more about this function.","rank_math_focus_keyword":"wp_update_post","footnotes":""},"categories":[22637],"tags":[],"class_list":["post-81039","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-use-wp-update-post-in-wordpress","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-use-wp_update_post","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-use-wp_update_post-to-update-wordpress-posts","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-use-wp_update_post-to-update-wordpress-posts","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-use-wp_update_post","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-use-wp-update-post-in-wordpress","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-use-wp-update-post-in-wordpress","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-use-wp-update-post-in-wordpress","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/81039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/users\/279"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/comments?post=81039"}],"version-history":[{"count":14,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/81039\/revisions"}],"predecessor-version":[{"id":144152,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/81039\/revisions\/144152"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/media?parent=81039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/categories?post=81039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/tags?post=81039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}