Use local post date#300
Conversation
|
@webelaine, Since the permalink is giving parameters to do a look up on post_date, then the month and year have to match that of the post_date, not post_date_gmt. The field in the database is datetime. This does not contain any timezone element and it will be written as the number in the writer's timezone. In this case, 31 october at 21:00:00. Thus we need 2022/10 in the permalink. However, I am concerned about the processing of this and the next line.
FWIW, my machine is normally set to Paris time. At 21:39, I created a post. In the database post_date is set as 21:39 and post_date_gmt is 20:39. I then set the WP instance to Chicago time. At 21:41, I created a new post. post_date was set to 14:41 and post_date_gmt to 20:41. I think that the field post_date - a string valiable - already contains the required data - and the timezone conversions just confuses things.. Certainly I have spent a considerable time trying to understand the various timestamp operations. I would like to propose for your consideration this replacement code: This avoids using timezone-related functions completely and extracts the required data directly. Kind Regards, |
Instead of converting the timestamp to a datetime object, converting it to UTC, and outputting the year and month and date from that, just grab the month and year directly from the post_date.
|
@NeilWJames - you raise an excellent point. I agree that pulling the month and year directly from the |
NeilWJames
left a comment
There was a problem hiding this comment.
Existing use of the gmt field is consistent, but gives permalink errors when the user's time zone is different to gmt.
This resolves the error in the determination of the month of the post.
Changes proposed
What this fixes
When a WordPress site uses a time zone other than UTC, and a new document is published on the last day of the month at a time when UTC falls in the next month, the plugin generates an invalid permalink with the UTC month rather than the month in the local timezone. This causes a 404 error for the document's permalink.
How to reproduce the bug
2022/11instead of2022/10.How to test the fix