Make WordPress Core

Opened 6 years ago

Closed 3 years ago

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

orderby datetime field

Reported by: saltnpixels Owned by:
Priority: normal Milestone:
Component: Query Version:
Severity: normal Keywords:
Cc: Focuses:

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
6 years ago

  • Component General → Query

#2 @decadx
6 years ago

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

#3 @Presskopp
5 years ago

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

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

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


4 years ago

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


3 years ago

#6 follow-up: @audrasjb
3 years 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
3 years 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
3 years 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 3 years ago by SergeyBiryukov (previous) (diff)

#9 @audrasjb
3 years ago

Alright let's remove this from the devhub explanation.

#10 @audrasjb
3 years ago

  • Milestone Awaiting Review
  • Resolution → reported-upstream
  • Status new → 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.