{"id":90526,"date":"2023-08-02T14:09:43","date_gmt":"2023-08-02T14:09:43","guid":{"rendered":"\/tutorials\/?p=90526"},"modified":"2024-03-26T04:48:36","modified_gmt":"2024-03-26T04:48:36","slug":"wp-insert-post","status":"publish","type":"post","link":"\/ph\/tutorials\/wp-insert-post","title":{"rendered":"How to Use the wp_insert_post Function in WordPress to Insert Custom Posts"},"content":{"rendered":"<p>The WordPress<strong> wp_insert_post<\/strong> function allows you to insert custom posts into your website&rsquo;s database using code. With this powerful tool, you can effortlessly create and manage dynamic content, surpassing the limitations of the standard post-creation process.<\/p><p>If you need to automate content generation, manage custom post types, or assign metadata and taxonomies via code, then <strong>wp_insert_post<\/strong> is the solution you&rsquo;ve been searching for.<\/p><p>This article will uncover the benefits of using the <strong>wp_insert_post<\/strong> function and explore its parameters. We&rsquo;ll also guide you through practical examples, illustrating how to use this function to create, customize, and manage various post types.<\/p><p>\n\n\n\n\n\n\n<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Mega-WordPress-Cheat-EN.pdf\" target=\"_blank\" rel=\"noopener\">Download all in one WordPress cheat sheet<\/a><\/p><h2 class=\"wp-block-heading\" id=\"h-why-use-the-wp-insert-post-function\">Why Use the wp_insert_post Function<\/h2><p>Creating and managing posts manually can be time-consuming, especially with a large content volume. It&rsquo;s particularly evident when <a href=\"\/ph\/tutorials\/wordpress-export-posts\/\">exporting your WordPress posts<\/a> to a new website.<\/p><p>Fortunately, with the <strong>wp_insert_post<\/strong> function, you can streamline your post-creation workflow and efficiently automate content generation. Not only does this save time, but it also minimizes the risk of human errors during data entry, ensuring your content is consistently accurate.<\/p><p>Additionally, <a href=\"\/ph\/tutorials\/wordpress\/\">WordPress<\/a> supports various post types for different purposes, such as blog posts, pages, products, or custom post types. The versatility of <strong>wp_insert_post<\/strong> lets you tailor your content structure to suit your website&rsquo;s unique requirements.<\/p><h2 class=\"wp-block-heading\" id=\"h-parameters-for-wp-insert-post\">Parameters for wp_insert_post<\/h2><p>Like other WordPress functions such as <a href=\"\/ph\/tutorials\/wordpress-get-posts-function\/\">get_posts<\/a>, the <strong>wp_insert_post<\/strong> function also has various parameters to facilitate custom post insertion. Here are some of the key parameters that you should be familiar with:<\/p><ul class=\"wp-block-list\">\n<li><strong>ID <\/strong>&ndash; The ID of the post to update. If provided, the function will attempt to edit the post with the given ID. Default is 0 (no update).<\/li>\n\n\n\n<li><strong>post_author <\/strong>&ndash; The ID of the post author. Default is the current user ID.<\/li>\n\n\n\n<li><strong>post_content <\/strong>&ndash; The content of the post. Default is an empty string.<\/li>\n\n\n\n<li><strong>post_content_filtered &ndash; <\/strong>The filtered post content. Default is an empty string.<\/li>\n\n\n\n<li><strong>post_title <\/strong>&ndash; The title of the post. Default is an empty string.<\/li>\n\n\n\n<li><strong>post_status <\/strong>&ndash; The status of the post (e.g., publish, draft, private). Default post status is publish.<\/li>\n\n\n\n<li><strong>post_type <\/strong>&ndash; The post type (e.g., post, page, custom post type). Default is post.<\/li>\n\n\n\n<li><strong>post_date <\/strong>&ndash; The date of the post. Default is the current date and time.<\/li>\n\n\n\n<li><strong>post_excerpt <\/strong>&ndash; The excerpt of the post. Default post excerpt is an empty string.<\/li>\n\n\n\n<li><strong>post_name <\/strong>&ndash; The post&rsquo;s slug (URL-friendly version of the title). Default is the sanitized post title.<\/li>\n\n\n\n<li><strong>post_parent <\/strong>&ndash; The ID of the parent post (if the post is a child of another post). Default post parent is 0 (no parent).<\/li>\n\n\n\n<li><strong>post_password <\/strong>&ndash; The password to protect the post (if password-protected). Default is an empty string.<\/li>\n\n\n\n<li><strong>post_category <\/strong>&ndash; An array of category IDs to assign to the post. Default is an empty array.<\/li>\n\n\n\n<li><strong>tags_input <\/strong>&ndash; An array of tags to assign to the post. Default is an empty array.<\/li>\n\n\n\n<li><strong>tax_input <\/strong>&ndash; An array of taxonomies and terms to assign to the post. Default is an empty array.<\/li>\n\n\n\n<li><strong>meta_input <\/strong>&ndash; An array of post meta values keyed by the post meta key. Default is an empty array.<\/li>\n<\/ul><p>The basic syntax of the <strong>wp_insert_post<\/strong> function is as follows:<\/p><pre class=\"wp-block-preformatted\">$post_id = wp_insert_post( $args, $wp_error );<\/pre><p><strong>$args<\/strong> is an array containing the post data and its corresponding parameters, while <strong>$wp_error<\/strong> is set to <strong>true <\/strong>to return a <strong>WP_Error <\/strong>object on failure. When executed, the function returns the ID of the newly inserted post on success or an error handling object otherwise.<\/p><p>\n\n\n        <div class=\"protip\">\n            <div class=\"protip__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path d=\"M1.49234 23.5024C1.23229 23.5024 0.972242 23.4024 0.782206 23.2123C0.562165 22.9923 0.452144 22.6822 0.502153 22.3722C0.562165 21.9221 1.14227 17.9113 3.00262 16.351C3.63274 15.8209 4.43289 15.5509 5.26305 15.5609C6.09321 15.5909 6.87335 15.9109 7.47347 16.4911C8.6937 17.6913 8.76371 19.6717 7.6435 20.9919C6.0832 22.8523 2.08245 23.4324 1.63237 23.4924C1.59236 23.4924 1.54235 23.4924 1.50234 23.4924L1.49234 23.5024ZM5.16303 17.5613C4.84297 17.5613 4.53291 17.6713 4.29287 17.8813C3.60274 18.4614 3.07264 19.9317 2.75258 21.242C4.06282 20.9219 5.5331 20.3918 6.11321 19.7017C6.55329 19.1716 6.54329 18.3814 6.0832 17.9213C5.85316 17.7013 5.5431 17.5713 5.20304 17.5613C5.19304 17.5613 5.17303 17.5613 5.16303 17.5613ZM11.7243 21.8821C11.4942 21.8821 11.2642 21.8021 11.0841 21.652C10.8541 21.462 10.7241 21.1819 10.7241 20.8819V15.9109L8.08358 13.2705H3.11264C2.81259 13.2705 2.53254 13.1404 2.3425 12.9104C2.15246 12.6803 2.07245 12.3803 2.12246 12.0902C2.19247 11.7102 2.84259 8.36953 4.70294 7.12929C6.33325 6.04909 8.96375 6.49918 10.244 6.80923C11.5442 4.96889 13.2546 3.4286 15.2349 2.33839C17.4553 1.11816 19.9858 0.518051 22.4963 0.498047C23.0464 0.498047 23.4865 0.948132 23.4865 1.49824C23.4865 5.0389 22.3763 9.97983 17.1753 13.7605C17.4853 15.0408 17.9354 17.6613 16.8552 19.2816C15.615 21.1419 12.2744 21.7921 11.8943 21.8621C11.8343 21.8721 11.7743 21.8821 11.7143 21.8821H11.7243ZM12.7245 16.181V19.6016C13.7146 19.2916 14.7948 18.7915 15.2049 18.1814C15.675 17.4812 15.605 16.091 15.385 14.9008C14.5248 15.3808 13.6346 15.8109 12.7245 16.181ZM9.66388 12.0302L11.9643 14.3307C13.1845 13.8306 14.3648 13.2204 15.485 12.5103C19.9358 9.51974 21.2361 5.60901 21.4561 2.53843C19.6157 2.67846 17.8254 3.20856 16.2051 4.09872C14.2847 5.14892 12.6544 6.68921 11.4942 8.54956C10.7841 9.65977 10.174 10.82 9.66388 12.0302ZM4.39289 11.2701H7.81353C8.1936 10.3599 8.63368 9.46974 9.11377 8.60957C7.92355 8.38953 6.51329 8.31952 5.81315 8.78961C5.19304 9.19968 4.70294 10.3099 4.39289 11.2701Z\" fill=\"#673DE6\"\/>\n                <\/svg>\n                <p class=\"protip__title\">\n                    Suggested Reading                <\/p>\n            <\/div>\n            <p class=\"protip__content\">For a complete list of all available <b>wp_insert_post<\/b> parameters, you can refer to the official  <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_insert_post\/\" target=\"_blank\" rel=\"nofollow noopener\">WordPress documentation<\/a> page.<\/p>\n                    <\/div>\n        \n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-use-the-wp-insert-post-function-in-wordpress\">How to use the wp_insert_post Function in WordPress<\/h2><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/ph\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/08\/The-wp-includes-post-php-file-is-located-in-the-file-manager.png\"><img decoding=\"async\" width=\"1024\" height=\"293\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/08\/The-wp-includes-post-php-file-is-located-in-the-file-manager.png\/w=1024,h=1024,fit=scale-down\" alt=\"The wp-includes\/post.php file is located in the File Manager\" class=\"wp-image-90527\" srcset=\"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-content\/uploads\/sites\/44\/2023\/08\/The-wp-includes-post-php-file-is-located-in-the-file-manager.png 1024w, https:\/\/www.hostinger.com\/ph\/tutorials\/wp-content\/uploads\/sites\/44\/2023\/08\/The-wp-includes-post-php-file-is-located-in-the-file-manager-300x86.png 300w, https:\/\/www.hostinger.com\/ph\/tutorials\/wp-content\/uploads\/sites\/44\/2023\/08\/The-wp-includes-post-php-file-is-located-in-the-file-manager-150x43.png 150w, https:\/\/www.hostinger.com\/ph\/tutorials\/wp-content\/uploads\/sites\/44\/2023\/08\/The-wp-includes-post-php-file-is-located-in-the-file-manager-768x220.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div><p>In this section, we&rsquo;ll see practical examples of using the wp_insert_post function to insert custom posts. You can add your custom code to the <strong>wp-includes\/post.php<\/strong> file to use this function. On <a href=\"\/ph\/wordpress-hosting\">Hostinger&rsquo;s WordPress plans<\/a>, the file can be accessed via the <strong>File Manager.<\/strong><\/p><figure class=\"wp-block-image size-large\"><a href=\"\/ph\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"2048\" height=\"600\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/44\/2024\/06\/New-WP_in-text-banner.png\/w=1024,h=1024,fit=scale-down\" alt=\"\" class=\"wp-image-111781\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/44\/2024\/06\/New-WP_in-text-banner.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/44\/2024\/06\/New-WP_in-text-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/44\/2024\/06\/New-WP_in-text-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/44\/2024\/06\/New-WP_in-text-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/44\/2024\/06\/New-WP_in-text-banner.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/44\/2024\/06\/New-WP_in-text-banner.png\/w=1536,fit=scale-down 1536w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><p>We&rsquo;ll explore three common scenarios to showcase this function&rsquo;s versatility and potential:<\/p><h3 class=\"wp-block-heading\" id=\"h-how-to-insert-a-post-and-check-for-errors\">How to Insert a Post and Check for Errors<\/h3><p>Let&rsquo;s start with a basic example of inserting a new post using the <strong>wp_insert_post<\/strong> function. The code below demonstrates how to validate the post data, handle errors, and provide feedback to users in case of any issues during the insertion process.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Include the necessary WordPress files\nrequire_once(ABSPATH . 'wp-admin\/includes\/post.php');\n\n\/\/ Create an array of post data for the new post\n$new_post = array(\n    'post_title'   =&gt; 'Sample Post Title', \/\/ Valid post name\n    'post_content' =&gt; 'This is a custom post with error handling.', \/\/ Unslashed post data - Set the content of the new post\n    'post_status'  =&gt; 'publish', \/\/ Unslashed post data - Set the status of the new post to 'publish'\n    'post_author'  =&gt; 1, \/\/ Replace with the desired author's user ID\n    'post_date'    =&gt; '2024-07-25 12:00:00', \/\/ Unslashed post data - Set a custom post date and time\n);\n\n\/\/ Insert post into the database\n$post_id = wp_insert_post($new_post, true); \/\/ Use $wp_error set to true for error handling\n\n\/\/ Check if there was an error during post insertion\nif (is_wp_error($post_id)) {\n    \/\/ Error occurred while inserting the post\n    echo \"Error: \" . $post_id-&gt;get_error_message();\n} else {\n    \/\/ The post was successfully inserted, and $post_id contains the post ID\n    echo \"Post inserted successfully. New Post ID: \" . $post_id;\n}<\/pre><p>If you want to update an existing post, you can utilize the code below:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Include the necessary WordPress files\nrequire_once(ABSPATH . 'wp-admin\/includes\/post.php');\n\n$updated_post = array(\n    'ID'           =&gt; $post_id, \/\/ Existing post ID\n    'post_content' =&gt; 'This is the updated content of the post.', \/\/ Updated post content\n);\n\n\/\/ Update post in the database\n$updated_post_id = wp_update_post($updated_post); \/\/ Updated post ID\n\n\/\/ Check for errors during post updating\nif (is_wp_error($updated_post_id)) { \/\/ Check if the result is a WP_Error object\n    $error_message = $updated_post_id-&gt;get_error_message(); \/\/ Retrieve the error message\n    \/\/ Handle the error, e.g., display an error message to the user\n    echo \"Error while updating the post: \" . $error_message;\n} else {\n    \/\/ The post was successfully updated\n    echo \"Post updated successfully. New Post ID: \" . $updated_post_id;\n}<\/pre><h3 class=\"wp-block-heading\" id=\"h-how-to-insert-a-new-post-into-a-wordpress-database\">How to Insert a New Post Into a WordPress Database<\/h3><p>Let&rsquo;s delve deeper into using the <strong>wp_insert_post<\/strong> function to insert new post data and explore additional parameters to set custom post fields such as the post author, post date, and post category.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Include the necessary WordPress files\nrequire_once(ABSPATH . 'wp-includes\/post.php');\n\n\/\/ Create an array of post details for the new post\n$new_post = array(\n    'post_title'    =&gt; 'Test Post with Metadata', \/\/ Parsed post data - Set the title of the new post\n    'post_content'  =&gt; 'This is a custom post with metadata and taxonomy terms.', \/\/ Post content - Set the content of the new post\n    'post_type'     =&gt; 'post', \/\/ Processed post data - Set the post type to 'post'\n    'post_status'   =&gt; 'publish', \/\/ Post status - Set the status of the new post to 'publish'\n    'post_author'   =&gt; 1, \/\/ Replace with the desired author's user ID\n    'post_date'     =&gt; '2024-07-21 12:00:00', \/\/ Post date - Set a custom post date and time\n    'tags_input'    =&gt; array('tag1', 'tag2', 'tag3'), \/\/ Processed attachment post data - Set tags for the new post\n    'page_template' =&gt; 'custom-template.php', \/\/ Invalid page template - Set a custom page template for the post\n);\n\n\/\/ Insert WordPress post into the database\n$post_id = wp_insert_post($new_post); \/\/ Existing attachment post - Set the existing post ID for updating\n\n\/\/ Check if the post was successfully inserted\nif (!is_wp_error($post_id)) {\n    \/\/ The post was successfully inserted, and $post_id contains the post ID\n\n\/\/ Get the post meta field based on the post ID\n    $meta_value = get_post_meta($post_id, 'meta_key_name', true);\n\n    \/\/ Add custom post categories (replace with actual category IDs)\n    $post_categories = array(2, 4); \/\/ Filters attachment post data - Category IDs to assign to the post\n    wp_set_post_categories($post_id, $post_categories); \/\/ Set post categories\n\n    \/\/ Add custom post meta data\n    update_post_meta($post_id, '_custom_meta_key', 'Custom Meta Value'); \/\/ Replace '_custom_meta_key' with your custom meta key and set its value\n\n    echo \"Post inserted successfully. New Post ID: \" . $post_id;\n} else {\n    \/\/ Error occurred while inserting the post\n    echo \"Error: \" . $post_id-&gt;get_error_message();\n}<\/pre><h3 class=\"wp-block-heading\" id=\"h-how-to-insert-a-post-with-custom-taxonomy-and-post-meta-data\">How to Insert a Post With Custom Taxonomy and Post Meta Data<\/h3><p>This example includes inserting a post with custom taxonomy terms and additional post metadata. <a href=\"\/ph\/tutorials\/wordpress-taxonomy\/\">WordPress taxonomy<\/a> allows you to organize and categorize posts more flexibly.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Include the necessary WordPress files\nrequire_once(ABSPATH . 'wp-includes\/post.php');\n\n\/\/ Create an array of post data for the new post\n$new_post = array(\n    'post_title'    =&gt; 'Custom Post with Taxonomy and Meta Data', \/\/ Otherwise unmodified post data - Set the title of the new post\n    'post_content'  =&gt; 'This is a custom post with taxonomy and meta data.', \/\/ Post content - Set the content of the new post\n    'post_type'     =&gt; 'post', \/\/ Post type - Set the post type to 'post'\n    'post_status'   =&gt; 'pending', \/\/ Pending review posts - Set the status of the new post to 'pending' for review\n    'post_author'   =&gt; 1, \/\/ Replace with the desired author's user ID\n    'post_date'     =&gt; '2024-08-05 12:00:00', \/\/ Post date - Set a custom post date and time\n    'post_name'     =&gt; 'custom-post-taxonomy-meta', \/\/ Otherwise unmodified post data - Set the post slug\n);\n\n\/\/ Insert post into the database\n$post_id = wp_insert_post($new_post);\n\n\/\/ Check if the post was successfully inserted\nif (!is_wp_error($post_id)) {\n    \/\/ The post was successfully inserted, and $post_id contains the post ID\n\n    \/\/ Add custom post categories (replace with actual category IDs)\n    $post_categories = array(2, 4); \/\/ Category IDs to assign to the post\n    wp_set_post_categories($post_id, $post_categories); \/\/ Set post categories\n\n    \/\/ Add post meta data\n    update_post_meta($post_id, '_custom_meta_key', 'Custom Meta Value'); \/\/ Sanitized attachment post data - Replace '_custom_meta_key' with your custom meta key and set its value\n\n    \/\/ Add custom taxonomy terms (replace with actual taxonomy and term IDs)\n    wp_set_object_terms($post_id, array(3, 5), 'custom_taxonomy'); \/\/ Set custom taxonomy terms\n\n    echo \"Post inserted successfully with custom taxonomy and meta data. New Post ID: \" . $post_id;\n} else {\n    \/\/ Error occurred while inserting the post\n    echo \"Error: \" . $post_id-&gt;get_error_message();\n}<\/pre><p>\n\n\n        <div class=\"protip\">\n            <div class=\"protip__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path d=\"M1.49234 23.5024C1.23229 23.5024 0.972242 23.4024 0.782206 23.2123C0.562165 22.9923 0.452144 22.6822 0.502153 22.3722C0.562165 21.9221 1.14227 17.9113 3.00262 16.351C3.63274 15.8209 4.43289 15.5509 5.26305 15.5609C6.09321 15.5909 6.87335 15.9109 7.47347 16.4911C8.6937 17.6913 8.76371 19.6717 7.6435 20.9919C6.0832 22.8523 2.08245 23.4324 1.63237 23.4924C1.59236 23.4924 1.54235 23.4924 1.50234 23.4924L1.49234 23.5024ZM5.16303 17.5613C4.84297 17.5613 4.53291 17.6713 4.29287 17.8813C3.60274 18.4614 3.07264 19.9317 2.75258 21.242C4.06282 20.9219 5.5331 20.3918 6.11321 19.7017C6.55329 19.1716 6.54329 18.3814 6.0832 17.9213C5.85316 17.7013 5.5431 17.5713 5.20304 17.5613C5.19304 17.5613 5.17303 17.5613 5.16303 17.5613ZM11.7243 21.8821C11.4942 21.8821 11.2642 21.8021 11.0841 21.652C10.8541 21.462 10.7241 21.1819 10.7241 20.8819V15.9109L8.08358 13.2705H3.11264C2.81259 13.2705 2.53254 13.1404 2.3425 12.9104C2.15246 12.6803 2.07245 12.3803 2.12246 12.0902C2.19247 11.7102 2.84259 8.36953 4.70294 7.12929C6.33325 6.04909 8.96375 6.49918 10.244 6.80923C11.5442 4.96889 13.2546 3.4286 15.2349 2.33839C17.4553 1.11816 19.9858 0.518051 22.4963 0.498047C23.0464 0.498047 23.4865 0.948132 23.4865 1.49824C23.4865 5.0389 22.3763 9.97983 17.1753 13.7605C17.4853 15.0408 17.9354 17.6613 16.8552 19.2816C15.615 21.1419 12.2744 21.7921 11.8943 21.8621C11.8343 21.8721 11.7743 21.8821 11.7143 21.8821H11.7243ZM12.7245 16.181V19.6016C13.7146 19.2916 14.7948 18.7915 15.2049 18.1814C15.675 17.4812 15.605 16.091 15.385 14.9008C14.5248 15.3808 13.6346 15.8109 12.7245 16.181ZM9.66388 12.0302L11.9643 14.3307C13.1845 13.8306 14.3648 13.2204 15.485 12.5103C19.9358 9.51974 21.2361 5.60901 21.4561 2.53843C19.6157 2.67846 17.8254 3.20856 16.2051 4.09872C14.2847 5.14892 12.6544 6.68921 11.4942 8.54956C10.7841 9.65977 10.174 10.82 9.66388 12.0302ZM4.39289 11.2701H7.81353C8.1936 10.3599 8.63368 9.46974 9.11377 8.60957C7.92355 8.38953 6.51329 8.31952 5.81315 8.78961C5.19304 9.19968 4.70294 10.3099 4.39289 11.2701Z\" fill=\"#673DE6\"\/>\n                <\/svg>\n                <p class=\"protip__title\">\n                    Suggested Reading                <\/p>\n            <\/div>\n            <p class=\"protip__content\">Learn more about WordPress posts with our tutorials:<br>\n<a href=\"\/ph\/tutorials\/how-to-add-wordpress-related-posts\/\">How to Add WordPress Related Posts<\/a><br>\n<a href=\"\/ph\/tutorials\/wordpress\/how-to-schedule-posts-in-wordpress\/\">How to Schedule Posts in WordPress<\/a><br>\n<a href=\"\/ph\/tutorials\/how-to-add-a-post-to-a-page-in-wordpress\/\">How to Add a Post to a Page in WordPress<\/a><br>\n<a href=\"\/ph\/tutorials\/wordpress-get-post-meta\/\">How to Display Post Metadata Using WordPress get_post_meta Function<\/a><\/p>\n                    <\/div>\n        \n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>The <strong>wp_insert_post<\/strong> function empowers WordPress websites with seamless custom post insertion, streamlining the process of adding or updating content. WordPress site owners and developers ensure consistency and accuracy throughout their websites by utilizing this function.<\/p><p>Whether you&rsquo;re a seasoned WordPress developer looking to optimize your workflows or a beginner eager to expand your knowledge, understanding <strong>wp_insert_post<\/strong> is a valuable skill that will significantly benefit your WordPress website.<\/p><p>Moreover, you can leverage <strong>wp_insert_post<\/strong> with other WordPress features, such as the <a href=\"\/ph\/tutorials\/the-loop-wordpress\/\">WordPress Loop<\/a>, to dynamically power content display. This synergy allows you to provide exceptional experiences for your website&rsquo;s visitors.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The WordPress wp_insert_post function allows you to insert custom posts into your website&rsquo;s database using code. With this powerful tool, you can effortlessly create and manage dynamic content, surpassing the limitations of the standard post-creation process. If you need to automate content generation, manage custom post types, or assign metadata and taxonomies via code, then [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ph\/tutorials\/wp-insert-post\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":411,"featured_media":90126,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Use the wp_insert_post Function in WordPress","rank_math_description":"The wp_insert_post function for WordPress inserts a custom post without using the native post page. Check this article to learn how to use it.","rank_math_focus_keyword":"wp_insert_post","footnotes":""},"categories":[],"tags":[],"class_list":["post-90526","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-use-wordpress-wp-insert-post\/","default":1},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/wp-insert-post\/","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-insert-post\/","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/wp-insert-post\/","default":0},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/wp-insert-post\/","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/wp-insert-post\/","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-use-wordpress-wp-insert-post\/","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-use-wordpress-wp-insert-post\/","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-use-wordpress-wp-insert-post\/","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/wp-insert-post\/","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/wp-insert-post\/","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/wp-insert-post\/","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/posts\/90526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/users\/411"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/comments?post=90526"}],"version-history":[{"count":8,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/posts\/90526\/revisions"}],"predecessor-version":[{"id":100942,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/posts\/90526\/revisions\/100942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/media\/90126"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/media?parent=90526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/categories?post=90526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ph\/tutorials\/wp-json\/wp\/v2\/tags?post=90526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}