Check if post exists

Question

How can I check if post with name for example Weather exists? If not I want to create it.

function such_post_exists($title) {
global $wpdb;

$p_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) );

if ( !empty ( $title ) ) {
    return (int) $wpdb->query("SELECT FROM $wpdb->posts WHERE post_title = $p_title");
}

return 0;
}
0
, kilogram 2 years 2020-08-16T05:10:23-05:00 0 Answers 52 views 0

Leave an answer

Browse
Browse