函数文档

post_exists()

💡 云策文档标注

概述

post_exists() 函数用于基于标题、内容、日期和类型等条件检查数据库中是否存在匹配的文章。它返回文章ID(如果存在)或0(如果不存在),适用于避免重复创建文章或验证文章存在性。

关键要点

  • 函数接受五个参数:$title(必需,文章标题)、$content(可选,文章内容)、$date(可选,文章日期)、$type(可选,文章类型)、$status(可选,文章状态)。
  • 返回值为整数:匹配文章的文章ID,若无匹配则返回0。
  • 内部使用 sanitize_post_field() 和 wp_unslash() 处理输入,确保数据库查询安全。
  • 通过构建 SQL 查询,基于非空参数在 wp_posts 表中精确匹配条件。
  • 从 WordPress 2.0.0 版本引入,5.2.0 添加 $type 参数,5.8.0 添加 $status 参数。
  • 相关函数包括 sanitize_post_field()、wp_unslash()、wpdb::get_var() 和 wpdb::prepare()。

代码示例

// 检查标题为 "My Post Title" 的文章是否存在
$found_post = post_exists( "My Post Title", '', '', '' );
echo $found_post ? "Found post at id #$found_post" : "Can't find post!";

// 检查自定义文章类型 'news' 中标题为 "My Post Title" 的文章是否存在
$found_post = post_exists( "My Post Title", '', '', 'news' );
echo $found_post ? "Found post at id #$found_post" : "Can't find post!";

注意事项

  • 使用前需确保 post_exists() 函数已定义,否则可能引发错误。建议添加代码:if ( ! function_exists( 'post_exists' ) ) { require_once ABSPATH . 'wp-admin/includes/post.php'; }
  • 此函数基于精确匹配,参数为空时可能返回意外结果,建议至少提供 $title 参数以提高准确性。
  • 对于仅检查文章ID是否存在,可考虑使用 get_post_status() 函数作为替代方案。

📄 原文内容

Determines if a post exists based on title, content, date and type.

Parameters

$titlestringrequired
Post title.
$contentstringoptional
Post content.
$datestringoptional
Post date.
$typestringoptional
Post type.
$statusstringoptional
Post status.

Return

int Post ID if post exists, 0 otherwise.

Source

function post_exists( $title, $content = '', $date = '', $type = '', $status = '' ) {
	global $wpdb;

	$post_title   = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );
	$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
	$post_date    = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) );
	$post_type    = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) );
	$post_status  = wp_unslash( sanitize_post_field( 'post_status', $status, 0, 'db' ) );

	$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
	$args  = array();

	if ( ! empty( $date ) ) {
		$query .= ' AND post_date = %s';
		$args[] = $post_date;
	}

	if ( ! empty( $title ) ) {
		$query .= ' AND post_title = %s';
		$args[] = $post_title;
	}

	if ( ! empty( $content ) ) {
		$query .= ' AND post_content = %s';
		$args[] = $post_content;
	}

	if ( ! empty( $type ) ) {
		$query .= ' AND post_type = %s';
		$args[] = $post_type;
	}

	if ( ! empty( $status ) ) {
		$query .= ' AND post_status = %s';
		$args[] = $post_status;
	}

	if ( ! empty( $args ) ) {
		return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
	}

	return 0;
}

Changelog

Version Description
5.8.0 Added the $status parameter.
5.2.0 Added the $type parameter.
2.0.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Check if post exist by title :

    $fount_post = post_exists( "My Post Title",'','','');
    	echo $fount_post ? "Found post at id #$fount_post" : "Can't find post!";

    Check if custom post type [news] exist by title :

    $fount_post = post_exists( "My Post Title",'','','news');
    	echo $fount_post ? "Found post at id #$fount_post" : "Can't find post!";