我想创建一个从前端到登录用户插入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 );
您可以通过检查某些条件来防止这种情况。你应该试试这个。在此条件下包装代码,它将不允许具有相同标题的帖子。
if (!get_page_by_title($title, 'OBJECT', 'post') ){
//your code goes here.
}