[WordPress]get_post_by_post_name

public function get_post_by_post_name($post_name, $post_type = '')
{
    global $wpdb;
    $sql = array();
    $sql[] = "SELECT";
    $sql[] = "p.*";
    $sql[] = "FROM $wpdb->posts AS p";
    $sql[] = "WHERE p.post_status = 'publish' AND p.post_name = '$post_name'";
    if (!empty($post_type))
        $sql[] = "AND p.post_type = '$post_type'";
    $result = $wpdb->get_results(implode(' ', $sql));
    if (empty($result))
        return null;
    return $result[0];
}