Alex Barber | Digital Artist

time to push the pixels

Sort custom post archive by ACF datetime field

function sort_events( $query ) {

// do not modify queries in the admin
if( is_admin() ) {

return $query;

}

// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'event' ) {

$query->set('meta_key', 'start');
$query->set('meta_type', 'DATETIME');
$query->set('orderby', 'meta_value');
$query->set('order', 'DESC');

}

// return
return $query;

}
add_action('pre_get_posts', 'sort_events');

Next Post

Leave a Reply

© 2017 Alex Barber | Digital Artist