开发者

wordpress num rows, how?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜