WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 5 months 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 (1)

#1 @SergeyBiryukov
5 months ago

  • Component changed from General to Query
Note: See TracTickets for help on using tickets.