I'm creating a plugin for wordpress and I need to check if a post with a specific title exist or not. Thi开发者_如何学Pythons to prevent duplicates, how can I query this and see if it returned true or false?
I’m using this code to get the ID of a post/page by title:
function get_id_by_name($name)
{
global $wpdb;
$id = $wpdb->get_col(
"SELECT ID FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_title = '$name'
LIMIT 1");
return empty ( $id ) ? -1: (int) $id[0];
}
If the post doesn’t exists, the function return -1.
精彩评论