【问题标题】:wordpress wp_insert_post is taking too longwordpress wp_insert_post 耗时太长
【发布时间】:2013-03-29 22:31:27
【问题描述】:

我正在使用一个简单的 php 文件发布到我的 wordpress,到目前为止我有大约 900 个帖子,但我注意到发布所需的时间越来越长!有时它甚至会用完(30秒+)!这是我使用的代码。

 <?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 的部分需要剩下的执行时间..

有人有优化这个的建议吗?

【问题讨论】:

  • 仪表板上还会出现这种情况吗?
  • @RobertLee 是仪表板相同
  • 您是否尝试过优化您的数据库?您可以进行手动查询,但这个更容易使用wordpress.org/extend/plugins/…
  • 您如何将要执行的脚本排队?也许您可以在两次运行之间将它们间隔 0.1 秒。

标签: php database wordpress


【解决方案1】:

找到解决方案。 我的一个插件造成了这种延迟。一旦我禁用了所有插件,延迟就是goan!。

我认为某些插件向 wp_new_post 添加了过滤器/挂钩,而该挂钩导致了此延迟问题。

为未来的谷歌员工。 只需禁用所有插件并尝试发布,如果问题是goan然后开始一一启用它们,直到找到导致此问题的插件。

感谢所有cmets和帮助

【讨论】:

  • 感谢对我帮助很大,对我来说是插件“WP-Mail-SMTP”。我在“wp_insert_post”钩子上挂了一封电子邮件,并且 smtp 配置错误。禁用它解决了我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
  • 2018-02-27
  • 2013-05-28
  • 2014-05-30
相关资源
最近更新 更多