{"id":2050,"date":"2022-05-02T14:43:26","date_gmt":"2022-05-02T12:43:26","guid":{"rendered":"https:\/\/www.wp-moon.com\/?p=2050"},"modified":"2022-05-02T14:43:30","modified_gmt":"2022-05-02T12:43:30","slug":"create-posts-pages-programmatically","status":"publish","type":"post","link":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/","title":{"rendered":"How to create posts and pages programmatically"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">WordPress features allow you to <strong>add posts and pages<\/strong> <strong>easily<\/strong> with editor on your website. Do you know that it is also very simple if you want to make it programmatically for your custom theme or plugin?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can add posts, pages, and <strong>custom post types<\/strong> with only one function and a few parameters. Nothing more is required. Let&#8217;s see this code together.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a post with wp_insert_post function<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The function to use is <strong>wp_insert_post()<\/strong>, you can find the complete <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_insert_post\/\" rel=\"nofollow\">documentation here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is another way to <strong>create posts programmatically with SQL<\/strong> (with <code><em>$wpdb->prepare<\/em><\/code> &amp; <code><em>$wpdb->query<\/em><\/code>), but we don&#8217;t recommend this method since <strong>wp_insert_post should cover all your needs<\/strong>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Parameters and example are explained below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parameters wp_insert_post explained<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Example of a simple new post added programmatically:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$post_id = wp_insert_post( \r\n\t\tarray( \r\n\t\t\t'post_content'          => 'My content here', \r\n\t\t\t'post_category'         => array( 1, 2 ), \r\n\t\t\t'post_title'            => 'My title here', \r\n\t\t\t'post_status'           => 'publish',\n\t\t\t'post_author'           => 1\r\n\t\t) \r\n\t);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can see that only five parameters (and even less) can do the job!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example above, the variable <em>$post_id<\/em> will return <strong>the new post ID<\/strong>, or false if the post cannot be created. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see every import parameter:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>ID<\/strong>: If you want to create a new post, do not include this parameter, it will be generate automatically. If you provide an existing ID of a post, this post will be updated with the new content provided<\/li><li><strong>post_author<\/strong>: The author ID. If you do not provide this parameter, the post will not be assign to any WordPress author.<\/li><li><strong>post_date<\/strong>: If you don&#8217;t provide this parameter, it will be the current date. If you want another date you should put something in this format <code>date( 'Y-m-d H:i:s', time() )<\/code><\/li><li><strong>post_content<\/strong>: the post content<\/li><li><strong>post_title<\/strong>: the post title<\/li><li><strong>post_excerpt<\/strong>: the post excerpt<\/li><li><strong>post_type<\/strong>: your post type . Default will be &#8220;post&#8221;, you can also set &#8220;page&#8221; or your custom post type<\/li><li><strong>post_category<\/strong>: Your categories ids into an array<\/li><li><strong>tags_input<\/strong>:  Your tags ids into an array. You can also put tag names or slugs<\/li><li><strong>post_status<\/strong>: Your post status. For example, <em>publish <\/em>(default), <em>draft <\/em>or <em>future<\/em><\/li><li><strong>post_parent<\/strong>: ID post parent if provided<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If you need more options, you should <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_insert_post\/#parameters\">read the description here<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Where to put this code ?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Usages of WordPress suggest adding your custom features <strong>into a new custom plugin <\/strong>(and not into the theme).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you add your code into your theme functions.php file, it will work without any problem if this is not the most recommended.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use a hook to insert a new post, most hooks are <a href=\"https:\/\/codex.wordpress.org\/Plugin_API\/Action_Reference\">listed here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WordPress features allow you to add posts and pages easily with editor on your website. Do you know that it is also very simple if you want to make it programmatically for your custom theme or plugin? You can add posts, pages, and custom post types with only one function and a few parameters. Nothing &hellip; <a href=\"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to create posts and pages programmatically<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":2051,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[147],"tags":[],"class_list":["post-2050","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to create posts and pages programmatically - WP Moon<\/title>\n<meta name=\"description\" content=\"WordPress features allow you to add posts and pages easily with editor on your website. Do you know that it is also very simple if you want to make it\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create posts and pages programmatically - WP Moon\" \/>\n<meta property=\"og:description\" content=\"WordPress features allow you to add posts and pages easily with editor on your website. Do you know that it is also very simple if you want to make it\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/\" \/>\n<meta property=\"og:site_name\" content=\"WP Moon\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/wpmoonwebsite\" \/>\n<meta property=\"article:published_time\" content=\"2022-05-02T12:43:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-05-02T12:43:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/05\/wordpress-265132_1280.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"853\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Alexandre G\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@alexgaboriau\" \/>\n<meta name=\"twitter:site\" content=\"@WPMoon4\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alexandre G\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create posts and pages programmatically - WP Moon","description":"WordPress features allow you to add posts and pages easily with editor on your website. Do you know that it is also very simple if you want to make it","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/","og_locale":"en_US","og_type":"article","og_title":"How to create posts and pages programmatically - WP Moon","og_description":"WordPress features allow you to add posts and pages easily with editor on your website. Do you know that it is also very simple if you want to make it","og_url":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/","og_site_name":"WP Moon","article_publisher":"https:\/\/www.facebook.com\/wpmoonwebsite","article_published_time":"2022-05-02T12:43:26+00:00","article_modified_time":"2022-05-02T12:43:30+00:00","og_image":[{"width":1280,"height":853,"url":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/05\/wordpress-265132_1280.jpg","type":"image\/jpeg"}],"author":"Alexandre G","twitter_card":"summary_large_image","twitter_creator":"@alexgaboriau","twitter_site":"@WPMoon4","twitter_misc":{"Written by":"Alexandre G","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/#article","isPartOf":{"@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/"},"author":{"name":"Alexandre G","@id":"https:\/\/www.wp-moon.com\/en\/#\/schema\/person\/72a64e6910977ec9e4b32fd8da54369d"},"headline":"How to create posts and pages programmatically","datePublished":"2022-05-02T12:43:26+00:00","dateModified":"2022-05-02T12:43:30+00:00","mainEntityOfPage":{"@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/"},"wordCount":421,"commentCount":0,"publisher":{"@id":"https:\/\/www.wp-moon.com\/en\/#organization"},"image":{"@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/05\/wordpress-265132_1280.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/","url":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/","name":"How to create posts and pages programmatically - WP Moon","isPartOf":{"@id":"https:\/\/www.wp-moon.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/#primaryimage"},"image":{"@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/#primaryimage"},"thumbnailUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/05\/wordpress-265132_1280.jpg","datePublished":"2022-05-02T12:43:26+00:00","dateModified":"2022-05-02T12:43:30+00:00","description":"WordPress features allow you to add posts and pages easily with editor on your website. Do you know that it is also very simple if you want to make it","breadcrumb":{"@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/#primaryimage","url":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/05\/wordpress-265132_1280.jpg","contentUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2022\/05\/wordpress-265132_1280.jpg","width":1280,"height":853,"caption":"create a new post with wordpress"},{"@type":"BreadcrumbList","@id":"https:\/\/www.wp-moon.com\/en\/create-posts-pages-programmatically\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.wp-moon.com\/en\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"https:\/\/www.wp-moon.com\/en\/category\/tutorials\/"},{"@type":"ListItem","position":3,"name":"How to create posts and pages programmatically"}]},{"@type":"WebSite","@id":"https:\/\/www.wp-moon.com\/en\/#website","url":"https:\/\/www.wp-moon.com\/en\/","name":"WP Moon","description":"","publisher":{"@id":"https:\/\/www.wp-moon.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wp-moon.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.wp-moon.com\/en\/#organization","name":"WP Moon","url":"https:\/\/www.wp-moon.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wp-moon.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2020\/02\/logo.png","contentUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2020\/02\/logo.png","width":290,"height":95,"caption":"WP Moon"},"image":{"@id":"https:\/\/www.wp-moon.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/wpmoonwebsite","https:\/\/x.com\/WPMoon4"]},{"@type":"Person","@id":"https:\/\/www.wp-moon.com\/en\/#\/schema\/person\/72a64e6910977ec9e4b32fd8da54369d","name":"Alexandre G","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2023\/09\/312419460_10229483707296045_2919774527885824513_n-100x100.jpg","url":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2023\/09\/312419460_10229483707296045_2919774527885824513_n-100x100.jpg","contentUrl":"https:\/\/www.wp-moon.com\/wp-content\/uploads\/2023\/09\/312419460_10229483707296045_2919774527885824513_n-100x100.jpg","caption":"Alexandre G"},"description":"Alexandre, Web Developer &amp; Website Editor. I've been using WordPress for over 10 years in my professional environment and on personal projects. I created WP Moon to teach you how to use this CMS, solve your problems and show you all the tricks.","sameAs":["https:\/\/www.croque-pixel.com\/","https:\/\/x.com\/alexgaboriau","https:\/\/www.youtube.com\/@wp_moon\/videos"],"url":"https:\/\/www.wp-moon.com\/en\/author\/wp-moon\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/posts\/2050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/comments?post=2050"}],"version-history":[{"count":0,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/posts\/2050\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/media\/2051"}],"wp:attachment":[{"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/media?parent=2050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/categories?post=2050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wp-moon.com\/en\/wp-json\/wp\/v2\/tags?post=2050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}