From 7b1a2648b3e02528133a7b7a0ac3c6ede8dd68d9 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 12 Apr 2005 21:12:28 +0000 Subject: [PATCH] Better page and subpage support for url_to_postid(). http://mosquito.wordpress.org/view.php?id=1214 Props: MC_incubus git-svn-id: http://svn.automattic.com/wordpress/trunk@2527 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index a918f903c..2f017a514 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -185,9 +185,9 @@ function url_to_postid($url = '') { if ($uri == $siteurl) return 0; - // First, check to see if there is a 'p=N' to match against: - preg_match('#[?&]p=(\d+)#', $uri, $values); - $p = intval($values[1]); + // First, check to see if there is a 'p=N' or 'page_id=N' to match against: + preg_match('#[?&](p|page_id)=(\d+)#', $uri, $values); + $p = intval($values[2]); if ($p) return $p; // Match $uri against our permalink structure @@ -235,7 +235,7 @@ function url_to_postid($url = '') { // If using %post_id%, we're done: if (intval($post_id)) return intval($post_id); - + // Otherwise, build a WHERE clause, making the values safe along the way: if ($year) $where .= " AND YEAR(post_date) = '" . intval($year) . "'"; if ($monthnum) $where .= " AND MONTH(post_date) = '" . intval($monthnum) . "'"; @@ -250,6 +250,14 @@ function url_to_postid($url = '') { return false; } + // if all we got was a postname, it's probably a page, so we'll want to check for a possible subpage + if ($postname && !$year && !$monthnum && !$day && !$hour && !$minute && !$second) { + $postname = rtrim(strstr($uri, $postname), '/'); + $uri_array = explode('/', $postname); + $postname = $uri_array[count($uri_array) - 1]; + $where = " AND post_name = '" . $wpdb->escape($postname) . "' "; + } + // Run the query to get the post ID: $id = intval($wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE 1 = 1 " . $where));