Ordering (Sorting) Custom Queries III Using Function.php


//Query For Archive-Event.php Page Start
function custom_event_adjust_queries($query){
    //if only is NOT Admin And Only On archive-event.php page And Only for the event $query
    if (!is_admin() AND is_post_type_archive('event') AND $query->is_main_query()) {
        $today = date('Ymd');
        $query->set('meta_key', 'event_date');
        $query->set('orderby', 'meta_value');
        $query->set('order', 'ASC');
        $query->set('meta_query', array(
            array(
              'key' => 'event_date',
              'compare' => '>=',
              'value' => $today,
              'type' => 'numeric'
            )
          ));
    }
}
//Trigger Function 
add_action('pre_get_posts', 'custom_event_adjust_queries');
//Query For Archive-Event.php Page End
Scroll to Top