I have an id:
$id = 151;
I want to check existence like this:
$content = get_post($id);
if ($content)
echo "post $id already exists";
else
echo "post $id does not exists or was deleted";
But in the WP forums always seems prefer ask to DB:
global $wpdb;
$post_exists = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE id = '" . $id . "'", 'ARRAY_A');
if ($post_exists)
echo "post $id exists";
else
echo "post $id does not exist";
So which is the best method? Indeed I prefer the ease of the first one.
if( is_null(get_post($id))){
echo "post $id does not exists or was deleted";
}else{
echo "post $id already exists";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With