Make WordPress Core

Opened 20 years ago

Closed 20 years ago

#1214 closed defect (bug) (fixed)

url_to_postid() for subpages returns ID of its parent, fails without rewrite rules

Reported by: mc_incubus's profile MC_incubus Owned by: ryan's profile ryan
Milestone: Priority: normal
Severity: major Version: 1.5.1
Component: General Keywords:
Focuses: Cc:

Description

the url_to_postid() for subpages returns the ID of its parent page.

e.g. /page/ id=1 and /page/subpage/ id=2
url_to_postid('http://site.com/page/subpage/') returns 1.

Same happens for /index.php/ type permalinks

when using no permalink rewrite style, http://site.com/?page_id=2, the ID is not properly extracted (need to add similar code to the code that extracts ?p=X type permalinks.)

Attachments (1)

functions.php.diff (1.5 KB) - added by MC_incubus 20 years ago.

Download all attachments as: .zip

Change History (6)

#1 @MC_incubus
20 years ago

  • Patch set to No

#2 @MC_incubus
20 years ago

functions.php.diff (592 bytes) fixes the page_id=X situation. I'm going to take a stab at fixing the other thing, but I might be in over my head!

#3 @MC_incubus
20 years ago

  • Owner changed from anonymous to rboren
  • Patch changed from No to Yes
  • Status changed from new to assigned

functions.php.diff (1,512 bytes) fixes both. I'd appreciate it if someone looked this over to see if there isn't a better way to do it.

for the page_id situation, I modified this:

preg_match('#?&p=(\d+)#', $uri, $values);
$p = intval($values[1]);
if ($p) return $p;

to this:

preg_match('#?&(p|page_id)=(\d+)#', $uri, $values);
$p = intval($values[2]);
if ($p) return $p;

The problem with subpage URIs was that the first "/post-slug/" was being grabbed, and everything after was dropped. So I added code that tests to see if only a post_name was found, which means that it's a page. If it is a page, it grabs "/post-slug/possible-subpage/grandchild-page/", strips off the trailing "/", uses explode() to put it into an array, and grabs the last value as $postname (in this case, it would be "grandchild-page") and then it overwrites the $where.

Works for me.

#4 @ryan
20 years ago

That looks good. We also need to support %category%. Also, using $wp_rewrite->rewritecode and $wp_rewrite->rewritereplace instead of local copies will keep the regular expressions up-to-date. The regular expressions used in url_to_postid() are old ones that will stomp i18n URIs.

#5 @ryan
20 years ago

  • fixed_in_version set to 1.5.1
  • Resolution changed from 10 to 20
  • Status changed from assigned to closed
Note: See TracTickets for help on using tickets.