每次页面加载时,wp_insert_post都会创建重复的帖子

时间:2014-03-09 18:02:31

标签: php wordpress wordpress-plugin

下面的代码运行并在每次页面加载时创建重复的帖子(有时是2个和4个重复帖子的批次),即使我已经检查过标题是否已经存在...

//checking to see if the title already exists.
if( null == get_page_by_title( $title, 'OBJECT', 'post' ) ) {
// Create post object
$my_post = array(
  'post_title'    => 'My post',
  'post_content'  => 'This is my post.',
  'post_status'   => 'publish'
);

// Insert the post into the database
wp_insert_post( $my_post );
}

我能以任何方式阻止这种行为,以便我可以安排时间和安排博客发布吗?

1 个答案:

答案 0 :(得分:1)

get_page_by_title的第二个参数应该是常量。不是字符串。

if( null == get_page_by_title( $title, OBJECT, 'post' ) ) {
...
}

让我们在深入挖掘之前抛弃它。