检查 post_content 是否已存在于数据库中
Check if post_content already exist in database
我需要一个函数来检查 post_content 是否已经存在于数据库中。
Wordpress 内置函数 post_exists() 通过 post post_title 检查。
我需要通过 post_content 检查,而不考虑 post_title。
有这样的功能吗?
我该如何解决这个问题?
感谢您的帮助
看起来 post_exists() 的一个小变体应该可以工作。在您的子主题的 functions.php 中创建这样的函数,然后使用它代替 post_exists():
function post_exists_by_content($content) {
global $wpdb;
$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
$args = array();
if ( !empty ( $content ) ) {
$query .= ' AND post_content = %s';
$args[] = $post_content;
}
if ( !empty ( $args ) )
return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
return 0;
}
我需要一个函数来检查 post_content 是否已经存在于数据库中。
Wordpress 内置函数 post_exists() 通过 post post_title 检查。
我需要通过 post_content 检查,而不考虑 post_title。
有这样的功能吗?
我该如何解决这个问题?
感谢您的帮助
看起来 post_exists() 的一个小变体应该可以工作。在您的子主题的 functions.php 中创建这样的函数,然后使用它代替 post_exists():
function post_exists_by_content($content) {
global $wpdb;
$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
$args = array();
if ( !empty ( $content ) ) {
$query .= ' AND post_content = %s';
$args[] = $post_content;
}
if ( !empty ( $args ) )
return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
return 0;
}