2013-04-07 158 views
1

我正在使用一個簡單的php文件發佈到我的wordpress,到目前爲止我有大約900posts,但我注意到它需要的時間越來越長!有時甚至用完(30秒+)!這裏是我使用的代碼。wordpress wp_insert_post花費太長時間

<?php 
    require_once('./../wp-blog-header.php'); 
    require_once('./simple_html_dom.php'); 
    require_once('./../wp-admin/includes/taxonomy.php'); 

function postit($category,$date,$title,$content,$keys){ 
$cat=wp_create_category($category); 
$post = array(
    'comment_status' => 'open',// 'closed' means no comments. 
    'ping_status' => 'open', // 'closed' means pingbacks or trackbacks turned off 
    'post_author' => '1', //The user ID number of the author. 
    'post_category' => array($cat), //post_category no longer exists, try wp_set_post_terms() for setting a post's categories 
    'post_content' => $content, //The full text of the post. 
    'post_date'  => date('Y-m-d H:i:s',strtotime($date)), //The time post was made. 
    'post_date_gmt' => date('Y-m-d H:i:s',strtotime($date)), //The time post was made, in GMT. 
    'post_status' => 'publish', //Set the status of the new post. 
    'post_title'  => $title, //The title of your post. 
    'post_type'  => 'post', //You may want to insert a regular post, page, link, a menu item or some custom post type 
    'tags_input'  => $keys,//For tags. 
    'post_content_filtered' => '1', 
    'filter' => '1' 
); 

remove_filter('content_save_pre', 'wp_filter_post_kses'); 
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses'); 
$r=wp_insert_post($post ,$wp_error); 
add_filter('content_save_pre', 'wp_filter_post_kses'); 
add_filter('content_filtered_save_pre', 'wp_filter_post_kses'); 
return $r; 
} 

當我在探查跑此,

wp_create_category運行0.01秒。剩下的代碼爲0.8秒,而刪除過濾器和wp_insert_post部分則需要剩餘的執行時間。

任何人都有一個建議來優化它?

+0

這仍然發生在儀表板上嗎? – 2013-04-07 23:29:00

+0

@RobertLee yes儀表板相同 – Zalaboza 2013-04-07 23:39:34

+0

您是否嘗試過優化數據庫?有手動查詢,你可以做,但這一個是更容易使用http://wordpress.org/extend/plugins/tentblogger-optimize-wordpress-database-plugin/ – 2013-04-07 23:42:57

回答

2

找到解決辦法。它是我的插件之一,使這種延遲。一旦我禁用了所有插件,延遲就是goan!

我想一些插件添加了一個過濾器/鉤子到wp_new_post並且該鉤子引起了這個延遲問題。

爲未來的googlers。 只是禁用所有插件並嘗試發佈,如果問題是goan然後開始啓用他們一個接一個,直到你找到引起這個問題的插件。

感謝所有的意見和幫助

+0

謝謝,幫了我很多,對我來說這是插件「WP-Mail-SMTP」。我已經掛上了發送「wp_insert_post」掛鉤的電子郵件,並且smtp錯過了配置。禁用它已解決了我的問題 – 2017-03-27 10:33:44