我想创建系统,将wordpress帖子从前端插入到登录用户。但我已经多次由不同的用户添加了相同的标题帖子。
如果帖子标题已存在,我如何添加error message
这是我的代码
$u_id = get_current_user_id();
$my_post = array(
'post_title' => 'title',
'post_content' => 'this is post content',
'post_status' => 'publish',
'post_author' => $u_id,
'post_type' => 'customer_post',
);
$post_ID = wp_insert_post( $my_post );
答案 0 :(得分:1)
您可以通过检查某些条件来防止这种情况。你应该试试这个。在这种情况下包裹您的代码,它将不允许使用相同标题的帖子。
if (!get_page_by_title($title, 'OBJECT', 'post') ){
//your code goes here.
}