Opened 9 years ago
Last modified 4 weeks ago
#42148 new enhancement
url_to_postid plain permalinks for CPTs
| Reported by: | soulseekah | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Permalinks | Version: | 1.0 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
Would be nice to have url_to_postid working with plain permalinks for custom post types.
There's are currently some issues in url_to_postid where the wrong ID is returned for custom post type plain permalinks (query-based).
<?php
add_action( 'init', function() {
/** Register a post type */
register_post_type( 'findme', array(
'public' => true
) );
/** Create a post */
$post_id = wp_insert_post( array( 'post_type' => 'findme', 'post_status' => 'publish' ) );
$findme = get_permalink( $post_id );
$found = url_to_postid( $findme );
/** Guess it */
printf( "%s (%s) == %s (%s)", $post_id, $findme, $found, get_permalink( $found ) );
exit;
} );
30 (http://localhost:8080/?findme=30) == 0 () and 30 (http://localhost:8080/?findme=30) == 2 (http://localhost:8080/) if the frontpage is setup to point ot a post.
Why is it not working? Why is the frontpage post being returned? Let's see how the {{url_to_postid}} function works:
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) {
$id = absint($values[2]);
if ( $id )
return $id;
}
Then?
if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
$page_on_front = get_option( 'page_on_front' );
if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
return (int) $page_on_front;
}
}
Uh, wait what... already? So a short-circuit without checking the custom post types. And that's understandable, since there is inherently no support for custom post type ID mappings as pointed out by:
// Check to see if we are using rewrite rules $rewrite = $wp_rewrite->wp_rewrite_rules(); // Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options if ( empty($rewrite) ) return 0;
What stands in our way to find the URL earlier?
- The query var is not one of p, page_id, attachment_id
- The query var value for CPTs is not necessarily, and most often not numeric (the post_title)
A proposed solution would be to look at the query parameters much higher, maybe by injecting the custom ones, ones that support slugs as well, since WordPress sets the page_title for a CPT itself, so that \d+ check would fail.
Use case? Well, this was encountered when trying to paste plain oEmbed URLs for a custom post type (https://github.com/gravityview/GravityView/issues/927).
Change History (3)
This ticket was mentioned in PR #14304 on WordPress/wordpress-develop by @irozum.
4 weeks ago
#2
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
@rejaulalomkhan commented on PR #14304:
4 weeks ago
#3
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: https://github.com/WordPress/wordpress-develop/pull/14304
Environment
- WordPress: 7.2-alpha-63166-src (trunk 3effce4538 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
- Ran
Tests_Rewritewith the PR: all pass on single site (114 tests) and multisite (116 tests). - Reverted
src/wp-includes/rewrite.phpwith the PR's test kept:test_url_to_postid_with_plain_permalinks_and_custom_post_typefails. - 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): returns0❌
- non-hierarchical (
- ✅ 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 withget_page_by_path( $value, OBJECT, $post_type )when the post type is hierarchical (or passing the query var itself toWP_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.
Summary
url_to_postid()returned0(or the static front page ID) for custom post type URLs when plain permalinks are in use, e.g./?findme=slug, because onlyp,page_idandattachment_idquery 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_idcheck, resolving the post bypost_typeandname.Testing
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.