The post_exists() WordPress PHP function determines if a post exists based on its title, content, date, type, and status.
Usage
$post_id = post_exists($title, $content = '', $date = '', $type = '', $status = '');
Parameters
$title
(string) – Required. The post title.$content
(string) – Optional. The post content. Default: ”.$date
(string) – Optional. The post date. Default: ”.$type
(string) – Optional. The post type. Default: ”.$status
(string) – Optional. The post status. Default: ”.
More information
See WordPress Developer Resources: post_exists
Examples
Check if post exists by title
$found_post = post_exists("My Post Title"); echo $found_post ? "Found post at id #$found_post" : "Can't find post!";
Check if a custom post type (e.g., ‘news’) exists by title
$found_post = post_exists("My Post Title", '', '', 'news'); echo $found_post ? "Found post at id #$found_post" : "Can't find post!";
Check if a post exists by title and content
$found_post = post_exists("My Post Title", "This is the post content."); echo $found_post ? "Found post at id #$found_post" : "Can't find post!";
Check if a post exists by title, content, and date
$found_post = post_exists("My Post Title", "This is the post content.", "2023-05-04"); echo $found_post ? "Found post at id #$found_post" : "Can't find post!";
Check if a post exists by title, content, date, type, and status
$found_post = post_exists("My Post Title", "This is the post content.", "2023-05-04", 'post', 'publish'); echo $found_post ? "Found post at id #$found_post" : "Can't find post!";