Opened 5 years ago
Closed 21 months ago
#50081 closed defect (bug) (reported-upstream)
orderby datetime field
Reported by: | saltnpixels | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Query | Keywords: | |
Focuses: | Cc: |
Description
I was creating an event post type and wanted to order a query by event date, a custom field.
I was having trouble because the documentation says I can use an orderby => 'meta_value_datetime'.
This did not work.
<?php array( 'post_type' => 'event', 'posts_per_page' => - 1, 'order' => 'ASC', 'meta_key' => 'due_date', //the custom field saved as Y-m-d H:i:s. 'orderby' => 'meta_value_datetime', 'meta_type' => 'DATETIME', )
Is this a bug? After much playing around by adding and removing the meta_type, trying all different things I got it working simply by using meta_value
<?php array( 'post_type' => 'event', 'posts_per_page' => - 1, 'order' => 'ASC', 'meta_key' => 'due_date', //the custom field saved as Y-m-d H:i:s. 'orderby' => 'meta_value', 'meta_type' => 'DATETIME', )
Change History (10)
This ticket was mentioned in Slack in #core by presskopp. View the logs.
2 years ago
This ticket was mentioned in Slack in #core by presskopp. View the logs.
21 months ago
#6
follow-up:
↓ 8
@
21 months ago
It looks like a Docs (DevHub issue) as meta_value_datetime
doesn't exists.
I see it was added in the DevHub explanation by @leogermani 4 years ago. @leogermani do you remember the context of this addition?
#7
@
21 months ago
Some details from the Slack discussion above:
Sergey: There is a
meta_value_num
option for orderby, but I don't recall seeingmeta_value_datetime
before. As you noted, it does not appear in the WP core source code.
I see it mentioned on https://developer.wordpress.org/reference/classes/wp_query/ though, so this would likely need some investigation as to when it was added to the documentation and where it came from.
Presskopp: Using web.archive.org it must have been added after May, 25, 2019 and before Aug, 03, 2019, if that is helping.
#8
in reply to:
↑ 6
@
21 months ago
Replying to audrasjb:
I see it was added in the DevHub explanation by @leogermani 4 years ago. @leogermani do you remember the context of this addition?
It appears to be added in this revision, which included some changes transferred from Codex.
The original change was done on Codex in December 2015 by some other person, perhaps mistakenly. So I think we can remove this from the DevHub explanation.
Just confirming this, I had the same issue, sorting with the
meta_value_datetime
didn't work correctly.