WordPress判断文章标题重复

对于文章数多的博客,在写文章时,如果发布了重复标题的文章,是一件很尴尬的事情,那么下面我们通过一些代码来防止这个问题出现。
将下面这些代码,放到主题目录functions.php下即可。
[cc lang=”html”]
// 发表文章页面,前端抓取标题并使用AJAX发送请求
add_action( ‘admin_print_footer_scripts’, ‘duplicate_titles_enqueue_scripts’, 100 );
function duplicate_titles_enqueue_scripts() {
?>

posts WHERE post_status = ‘publish’ AND post_type = ‘post’
AND post_title = ‘{$title}’ AND ID != {$post_id} “;
$results = $wpdb->get_results($titles);
if($results) {
echo ““. _( ‘此标题已存在,请换一个标题!’ , ” ) .” “;
} else {
echo ‘‘._(‘恭喜,此标题未与其他文章标题重复!’ , ”).’‘;
}
die();
}

// 检测后端标题并且避免同名文章更新草稿
add_action( ‘publish_post’,’duplicate_titles_wallfa_bc’ ) ;
function duplicate_titles_wallfa_bc( $post ){
global $wpdb ;
$title = $_POST[‘post_title’] ;
$post_id = $post ;
$wtitles = “SELECT post_title FROM $wpdb->posts WHERE post_status = ‘publish’ AND post_type = ‘post’
AND post_title = ‘{$title}’ AND ID != {$post_id} ” ;
$wresults = $wpdb->get_results( $wtitles ) ;
if ( $wresults ){
$wpdb->update( $wpdb->posts, array( ‘post_status’ =>
‘draft’ ), array( ‘ID’ => $post ) ) ;
$arr_params = array( ‘message’ => ’10’, ‘wallfaerror’ => ‘1’ ) ;
$location = add_query_arg( $arr_params , get_edit_post_link( $post , ‘url’ ) ) ;
wp_redirect( $location ) ;
exit ;
}
}

/// 文章提交更新后的提示
add_action( ‘admin_notices’, ‘not_published_error_notice’ );
function not_published_error_notice() {
if(isset($_GET[‘wallfaerror’]) == 1 ){
?>

点赞
  1. RubenViags说道:
    Google Chrome 65.0.3325.181 Windows 10

    Rollex – 5Bintang Kasino http://www.5bintangkasino.com/rollex/ - Rollex – 5Bintang Kasino>>>

  2. RubenViags说道:
    Google Chrome 67.0.3396.87 Windows 8.1

    Mega888 – 5Bintang Kasino http://www.5bintangkasino.com/mega888/ - Click here...

  3. RubenViags说道:
    Microsoft Edge 17.17134 Windows 10

    Login 918Kiss – 5Bintang Kasino http://www.5bintangkasino.com/login-918kiss/ - Login 918Kiss – 5Bintang Kasino!..

发表评论

电子邮件地址不会被公开。 必填项已用*标注