Skip to content

Rewrite: Make url_to_postid() resolve custom post type query vars in plain permalinks - #14304

Open
irozum wants to merge 1 commit into
WordPress:trunkfrom
irozum:fix/42148-url-to-postid-plain-cpt-permalinks
Open

irozum wants to merge 1 commit into
WordPress:trunkfrom
irozum:fix/42148-url-to-postid-plain-cpt-permalinks

Conversation

@irozum

@irozum irozum commented Oct 15, 2026

Copy link
Copy Markdown

Summary

url_to_postid() returned 0 (or the static front page ID) for custom post type URLs when plain permalinks are in use, e.g. /?findme=slug, because only p, page_id and attachment_id query args were recognised, and the front page shortcut and the "no rewrite rules" bail-out ran before anything looked at the CPT query var.

This adds a check for public post type query vars right after the p/page_id/attachment_id check, resolving the post by post_type and name.

Trac ticket: https://core.trac.wordpress.org/ticket/42148

Testing

  • Added Tests_Rewrite::test_url_to_postid_with_plain_permalinks_and_custom_post_type (fails on trunk with a static front page returned, passes with the fix).
  • npm run test:php -- --filter Tests_Rewrite: OK (93 tests, 280 assertions). PHPCS shows no violations; PHPStan reports no errors.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Implementation, tests, and PR description.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props irozum.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@rejaulalomkhan

Copy link
Copy Markdown

Thanks for working on this, @irozum! Moving the check ahead of the front-page shortcut covers the case from the ticket nicely. I tested it locally:

Patch Testing Report

Patch tested: #14304

Environment

  • WordPress: 7.2-alpha-63166-src (trunk 3effce4 with the PR applied)
  • PHP: 8.3.35
  • Server: PHP CLI (PHPUnit 9.6.38, wordpress-develop Docker environment)
  • Database: MySQL 9.7.2
  • Browser: N/A (unit tests)
  • OS: macOS 27.0.1
  • Theme: N/A
  • MU Plugins: None activated
  • Plugins: None activated

Steps taken

  1. Ran Tests_Rewrite with the PR: all pass on single site (114 tests) and multisite (116 tests).
  2. Reverted src/wp-includes/rewrite.php with the PR's test kept: test_url_to_postid_with_plain_permalinks_and_custom_post_type fails.
  3. With plain permalinks, registered a non-hierarchical and a hierarchical public post type and called url_to_postid( get_permalink( $id ) ):
    • non-hierarchical (?zznote=zz-note): returns the post ID ✅
    • hierarchical parent (?zzbook=zz-parent): returns the post ID ✅
    • hierarchical child (?zzbook=zz-parent/zz-child): returns 0 ❌
  4. ✅ Patch is solving the problem for non-hierarchical post types and top-level posts; child posts of hierarchical post types are not resolved yet.

Expected result

  • url_to_postid() returns the post ID for custom post type URLs that use plain permalinks.

Additional Notes

  • For hierarchical post types the query var holds the full path, so 'name' => 'zz-parent/zz-child' matches nothing. Looking the post up with get_page_by_path( $value, OBJECT, $post_type ) when the post type is hierarchical (or passing the query var itself to WP_Query, as the front end does) should cover it; a data set for a child post would lock that in.
  • AI assistance: Claude Code was used to run the tests and the URL checks; results reviewed by me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants