我需要一个函数来检查数据库中是否已经存在 post_content。
Wordpress 内置函数 post_exists() 通过 post_title 检查。
无论 post_title 是什么,我都需要通过 post_content 检查。
有这样的功能存在吗?
我该如何解决这个问题?
谢谢您的帮助
我需要一个函数来检查数据库中是否已经存在 post_content。
Wordpress 内置函数 post_exists() 通过 post_title 检查。
无论 post_title 是什么,我都需要通过 post_content 检查。
有这样的功能存在吗?
我该如何解决这个问题?
谢谢您的帮助
看起来 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;
}