Make WordPress Core

Opened 4 years ago

Closed 11 months ago

#50081 closed defect (bug) (reported-upstream)

orderby datetime field

Reported by: saltnpixels's profile 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)

#1 @SergeyBiryukov
4 years ago

  • Component changed from General to Query

#2 @decadx
3 years ago

Just confirming this, I had the same issue, sorting with the meta_value_datetime didn't work correctly.

#3 @Presskopp
2 years ago

Seems like meta_value_datetime doesn't even appear in the sourcecode...

Last edited 2 years ago by Presskopp (previous) (diff)

This ticket was mentioned in Slack in #core by presskopp. View the logs.


14 months ago

This ticket was mentioned in Slack in #core by presskopp. View the logs.


11 months ago

#6 follow-up: @audrasjb
11 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 @SergeyBiryukov
11 months ago

Some details from the Slack discussion above:

Sergey: There is a meta_value_num option for orderby, but I don't recall seeing meta_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 @SergeyBiryukov
11 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.

Last edited 11 months ago by SergeyBiryukov (previous) (diff)

#9 @audrasjb
11 months ago

Alright let's remove this from the devhub explanation.

#10 @audrasjb
11 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to reported-upstream
  • Status changed from new to closed

I removed the mention of meta_value_datetime in the DevHub explanation.
Closing this ticket as reported-upstream as it was fixed without any Core change. Thanks everyone!

Note: See TracTickets for help on using tickets.