WordPress.org

Make WordPress Core

Opened 21 months ago

Last modified 8 weeks ago

#50081 new defect (bug)

orderby datetime field

Reported by: saltnpixels Owned by:
Milestone: Awaiting Review 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 (3)

#1 @SergeyBiryukov
21 months ago

  • Component changed from General to Query

#2 @decadx
14 months ago

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

#3 @Presskopp
8 weeks ago

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

Last edited 8 weeks ago by Presskopp (previous) (diff)
Note: See TracTickets for help on using tickets.