我想从我的 wordpress 网站的前端创建一个帖子。
当人们添加具有相同 post_title 的帖子时,我希望该帖子得到更新,而不是创建新帖子。
我有以下内容:
if (!get_page_by_title($post_title, 'OBJECT', 'post') ){
$my_post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_author' => $post_author,
'post_category' => $post_categories
);
wp_insert_post( $my_post );
}
else {
$page = get_page_by_title($post_title);
$page_id = $page->ID;
$my_post = array(
'ID' => $page_id,
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_author' => $post_author,
'post_category' => $post_categories
);
wp_update_post( $my_post );
}
以上工作正常,直到帖子标题相同。它仍然会在数据库中重复,甚至不会考虑“else”语句。
以上看起来不错,还是我做错了什么?