Title: meta_value_date &amp; sorting not working
Last modified: July 1, 2019

---

# meta_value_date & sorting not working

 *  Resolved [GusGF](https://wordpress.org/support/users/gusgf/)
 * (@gusgf)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/meta_value_date-sorting-not-working/)
 * I’m fetching a custom post type called ‘event’ and am using a plugin called ACF
   to add a custom field (of type ‘date picker’) within the WP editor for this post
   type. Using the query below I’m fetching ‘events’ and ordering them or trying
   to at least.
 *     ```
       $eventQuery = new WP_Query(array('post_type'=>'event', 
                                        'posts_per_page'=>-1,
                                        'order'=>'asc', 
                                        'orderby'=>'meta_value_date', 
                                        'meta_key'=>'event_date'));
       ```
   
 * The problem is the ‘events’ are not being sorted correctly unless I change the
   type from ‘meta_value_date’ to ‘meta_value_num’ for the sort. According to the
   documentation ‘date picker’ in ACF stores dates in the DB as ‘YYYYMMDD’ so one
   can see why ‘meta_value_num’ works but why does ‘meta_value_date’ equally not
   work as a WP query??

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Jacob Peattie](https://wordpress.org/support/users/jakept/)
 * (@jakept)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/meta_value_date-sorting-not-working/#post-11689811)
 * My reading of this line in [the documentation](https://developer.wordpress.org/reference/classes/wp_query/)
   for `WP_Query` implies to me that to use `meta_value_date` you need to set the`
   meta_type` argument to `DATE`:
 * > You may also specify `'meta_type'` if you want to cast the meta value as a 
   > specific type. … When using `'meta_type'` you can also use `'meta_value_*'`
   > accordingly. For example, when using `DATETIME` as `'meta_type'` you can use`'
   > meta_value_datetime'` to define order structure.
 * Which would look like this:
 *     ```
       $eventQuery = new WP_Query( array(
       	'post_type'      => 'event', 
       	'posts_per_page' => -1,
       	'order'          => 'ASC', 
       	'meta_key'       => 'event_date'
       	'meta_type'      => 'DATE',
       	'orderby'        => 'meta_value_date', 
       ) );
       ```
   
 * However I can’t imagine this being faster than just sorting numerically, if dates
   are stored as YYYYMMDD, so my suggestion would be to stick with `meta_value_num`.
    -  This reply was modified 7 years, 1 month ago by [Jacob Peattie](https://wordpress.org/support/users/jakept/).
 *  [Anil Ankola](https://wordpress.org/support/users/anilankola/)
 * (@anilankola)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/meta_value_date-sorting-not-working/#post-11695590)
 *     ```
       please used this args in your WP_Query
       meta_key args used the acf field name and orderby args used the meta_value
   
       'orderby'=>'meta_value', 
       'meta_key'=>'event_date',
       'order'=>'DESC', 
       ```
   
 *  Thread Starter [GusGF](https://wordpress.org/support/users/gusgf/)
 * (@gusgf)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/meta_value_date-sorting-not-working/#post-11715115)
 * Yes Jacob that does indeed seem to work 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘meta_value_date & sorting not working’ is closed to new replies.

## Tags

 * [meta_value)](https://wordpress.org/support/topic-tag/meta_value/)
 * [sorting](https://wordpress.org/support/topic-tag/sorting/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 3 participants
 * Last reply from: [GusGF](https://wordpress.org/support/users/gusgf/)
 * Last activity: [7 years, 1 month ago](https://wordpress.org/support/topic/meta_value_date-sorting-not-working/#post-11715115)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
