post_exists()

 

File: wp-admin/includes/post.php

function post_exists( $title, $content = '', $date = '' ) {
	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' ) );

	$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( $args ) ) {
		return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) );
	}

	return 0;
}

参数:post_exists( string $title, string $content = '', string $date = '' )

Post titlepost contentpost date

(int)post id如果post存在,则为0。

$title

(string)
(Required)
Post title

$content

(string)
(Optional)
post content

Default value: ''

$date

(string)
(Optional)
post date

Default value: ''

{acontent3}

You must log in before being able to contribute a note or feedback.

22.91% (63) 次Baidu访问
2.91% (8) 次Google访问
70.18% (193) 次Sogou访问
2.55% (7) 次bingbot访问
1.45% (4) 次sm访问
  • 蜘蛛:Baidu,63次,更新时间:2019-05-20 18:58:22
  • 蜘蛛:Google,8次,更新时间:2019-05-10 18:35:30
  • 蜘蛛:Sogou,193次,更新时间:2019-05-23 04:24:12
  • 蜘蛛:bingbot,7次,更新时间:2019-04-21 06:26:06
  • 蜘蛛:sm,4次,更新时间:2019-04-20 10:05:29

参与评论