How to hide past events posts based on custom end date field - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, August 24, 2023

How to hide past events posts based on custom end date field

Im using this code for sorting posts by a custom date field. "start date" = (dato_for_arrangementet)

function my_pre_get_posts( $query ) {
    
    // do not modify queries in the admin
    if( is_admin() ) {
        return $query;
    }

    // only modify queries for 'event' post type with order by date
    if( isset($query->query_vars['post_type']) && (is_array($query->query_vars['post_type']) && in_array('event', $query->query_vars['post_type'])) && $query->query['orderby'] == 'date')  {
        $query->set('orderby', 'meta_value');    
        $query->set('meta_key', 'dato_for_arrangementet');     
        $query->set('order', 'ASC'); 
    }

    // return
    return $query;
}

add_action('pre_get_posts', 'my_pre_get_posts'); type here

Q: if i add another custom field "End Date" how can i hide posts based on this enddate?



source https://stackoverflow.com/questions/76963882/how-to-hide-past-events-posts-based-on-custom-end-date-field

1 comment: