开发者

wordpress num rows, how?

开发者 https://www.devze.com 2022-12-25 17:29 出处:网络
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 o

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

精彩评论

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

关注公众号