• Hello

    I want to sort the order of appearance of articles by date of event. To make the event appear until the end I have to sort by end date (see code below). Is it possible to display the order by start date but that it remains visible until the end date?

    If anyone can find me the solution with the code below you will do me a huge service. Thanks a lot for your help ??

    function change_order_for_archives($query) {
    
        $today = current_time('mysql');
        $yesterday = date('Y-m-d H:i:s', strtotime($today . ' -1 day'));
    
    		if ($query->is_archive() && $query->is_main_query() && !is_admin()) {
    		    $query->set( 'meta_key', 'event_end' );
    		    $query->set( 'orderby', 'event_end' );
    		    $query->set( 'order', 'ASC' );
    		
    		    //Get events after 24 hours ago
    		    $query->set( 'meta_value', $yesterday );
    		    $query->set( 'meta_compare', '>' );
    		
    		   //Get events before now
    		   //$query->set( 'meta_value', current_time('timestamp') );
    		   //$query->set( 'meta_compare', '<' );
    		}
    }
    add_action( 'pre_get_posts', 'change_order_for_archives' );
    
    • This topic was modified 6 years, 2 months ago by vsmh.
  • The topic ‘display order of posts’ is closed to new replies.