{"id":524,"date":"2009-09-20T14:54:20","date_gmt":"2009-09-20T12:54:20","guid":{"rendered":"http:\/\/patchlog.com\/?p=524"},"modified":"2009-09-20T14:54:20","modified_gmt":"2009-09-20T12:54:20","slug":"fast-page-insert-in-wordpress","status":"publish","type":"post","link":"https:\/\/patchlog.com\/wordpress\/fast-page-insert-in-wordpress\/","title":{"rendered":"Fast page insert in wordpress"},"content":{"rendered":"<p>You want to create a lot of pages in wordpress using a script to populate a blog with content you might have. To make sure your script will be compatible with future versions of wordpress you want to use <a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/wp_insert_post\">wp_insert_post<\/a><\/p>\n<h3>The Problem<\/h3>\n<p>The more pages you add the slower your script will be. With about 700 pages my script took 12 seconds to add a new page.<br \/>\nAfter digging through the code I found out that wp_insert_code calls $wp_rewrite-&gt;flush_rules() every time a new post or page is inserted and that this is what takes the most time to finish.<br \/>\nNow it makes sense, the more pages you have the more rules ( permalinks ) you have and more time it will take to finish that function call.<\/p>\n<h3>The Solution<\/h3>\n<p>The call to $wp_rewrite-&gt;flush_rules() can be disabled by defining WP_IMPORTING. Now inserting a post takes just a second or less. But you still have to call $wp_rewrite-&gt;flush_rules()  after you're done inserting all posts. This call will take quite a lot depending on the total number of posts\/pages you have but it's a lot better to call it only once then a few hundreds or thousand times.<\/p>\n<p>The way wordpress updates it's rules needs to change. Even if we can solve the bulk import problem by calling flush_rules at the end , we end up with a blog with thousands of pages where trying to publish a new post manually might take 30 or more seconds.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You want to create a lot of pages in wordpress using a script to populate a blog with content you might have. To make sure your script will be compatible with future versions of wordpress you want to use wp_insert_post The Problem The more pages you add the slower your script will be. With about &hellip; <a href=\"https:\/\/patchlog.com\/wordpress\/fast-page-insert-in-wordpress\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Fast page insert in wordpress<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[12],"tags":[],"class_list":["post-524","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pofPh-8s","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts\/524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/comments?post=524"}],"version-history":[{"count":4,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts\/524\/revisions"}],"predecessor-version":[{"id":528,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/posts\/524\/revisions\/528"}],"wp:attachment":[{"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/media?parent=524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/categories?post=524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/patchlog.com\/wp-json\/wp\/v2\/tags?post=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}