tags:

views:

9

answers:

1

I'm creating a plugin for wordpress and I need to check if a post with a specific title exist or not. This to prevent duplicates, how can I query this and see if it returned true or false?

+1  A: 

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.

toscho
jep it works perfectly now :D thanks a lot, didn't know get_col would do the trick :)
krike