Opened 3 months ago
Last modified 2 months ago
#61969 new defect (bug)
Post guid for custom post type being modified
Reported by: | peter8nss | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | minor | Version: | 3.2 |
Component: | Posts, Post Types | Keywords: | has-patch |
Focuses: | Cc: |
Description
I understand that a post's guid should generally be invariant, i.e. once set it should not normally be changed. WordPress core seems to violate this for custom post types.
When creating a post for a custom post type the guid created is (now) of the form:
<domain>/?post_type=<custom post type>&=p=<post id>
When that post is first updated the guid gets changed to
<domain>/?post_type=<custom post type>&p=<post id>
I think this is happening because of the filter added on post_guid calling esc_url (see wp-includes/default-filters.php) which is called when updating the post but not when originally creating it.
Note the creating logic sets guid using get_permalink which in turn calls get_post_permalink and does not include any use of esc_url.
You can recreate the problem, providing you have a custom post type available, by the following WP CLI commands:
wp post create --post_title='Initial create' --post_type=<custom post type>
guid will be set as described above
wp post update <post id> --post_title='Updated title'
guid will be revised as described above
And you can use "wp post get <post id>" to see the different guid values
Naively, I would suggest two ways of fixing this:
1) Remove the default filter on post_guid that calls esc_url
2) Wrap the call to get_permalink in wp_insert_post in a call to esc_url
Change History (5)
#2
follow-up:
↓ 3
@
3 months ago
Filter on post_guid to call esc_url was added in:
Changeset 17994 05/22/2011 11:19:42 PM (13 years ago)
So I don't think this was recently introduced.
This ticket was mentioned in PR #7542 on WordPress/wordpress-develop by @debarghyabanerjee.
2 months ago
#5
- Keywords has-patch added
Trac Ticket: Core-61969
## Problem Statement
- In WordPress, a post's GUID is intended to be invariant once set. However, there is an inconsistency in how GUIDs are handled for custom post types.
- When a post is created for a custom post type, the GUID is generated in a specific format. Upon updating the post, the GUID changes due to the esc_url filter applied to the post_guid, which is executed during the update process but not during creation. This results in different GUID values for the same post, violating the principle of invariance.
## Solution
- This PR implements the second approach by wrapping the call to
get_permalink
in wp_insert_post withesc_url
, ensuring the GUID remains consistent and invariant during both post creation and updates.
## Changes Made
- Updated the wp_insert_post function to apply esc_url to the GUID generation process.
- Ensured that the change maintains backward compatibility with existing functionality.
## Benefits
- Resolves the inconsistency in GUID handling for custom post types.
- Maintains the integrity of the GUID, adhering to the principle of invariance.
- Enhances code reliability and improves the user experience by preventing unintended GUID changes.
## Testing
- Verified the changes by creating and updating custom post types, ensuring the GUID remains consistent.
- Conducted unit tests to confirm that no other functionality was affected by the modifications.
Hello @peter8nss,
Welcome to WordPress Core's Trac.
I'm doing 6.6.x triage today and updating tickets. I'm resetting the
Version
as I don't think any code was changed in either 6.6.1 or 6.6.2 that might be affecting the reported issue. Once the root cause is identified, then theVersion
can be updated to reflect the version that introduced the issue.