Support

Account

Home Forums General Issues Event Date Sorting

Solved

Event Date Sorting

  • Hey all,

    I know a lot of you have used the ever-wonderful ACF to set up events calendars, and now it’s my turn!

    I’ve used the following code and the pre_get_posts filter to hide past events within the relevant archive page, and it’s working brilliantly.

    However, I’ve got certain events that don’t have an “end_date” value associated with them, and I’d still like to show these – the problem I have is that with the query shown here, they are being hidden.

    Based on this, I was wondering if any of you might know how to modify the query so that events with the “end_date” field are shown in order as before, but those without still get shown within the archive?

    To hopefully help with this, I’ve created an “event_date” field in ACF which has three possible values: “one-off”, “recurring” and “tbc” – only Posts with “one-off” set have an “end_date” if that makes sense?

    I will very much look forward to hearing of any thoughts you might have!

    Alex

    function event_cpt_query($query){
    	
    		if($query->is_main_query() && !$query->is_feed() && !is_admin() && $query->is_tax('type','talks')){
    			
    			$today = date('Ymd');
    			
    			$meta_query = array(
    
    				array(
    					'key' => 'end_date',
    					'compare' => '>=',
    					'value' => $today,
    				),
    				
    			);
    
    			$query->set( 'meta_query', $meta_query );
    			$query->set( 'orderby', 'meta_value_num' );
    			$query->set( 'meta_key', 'end_date' );
    			$query->set( 'order', 'ASC' );
    			
    		}
    		
    	}
    	
    	add_action( 'pre_get_posts', 'event_cpt_query' );
  • It really depends on what else your displaying posts based on when there is no end_date, but you can get all values > today or posts that do not have end_date set something like this

    
    $meta_query = array(
      'relation' => 'OR',
      array(
        'key' => 'end_date',
        'compare' => '>=',
        'value' => $today,
      ),
      array(
        'key' => 'end_date',
        'compare' => 'NOT EXIST'
      ),
    );
    

    https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

  • Hi John,

    Thanks so much for your help with this; hugely appreciated!

    As it happens, no sooner than I had posted my question on these here boards, I came up with the following solution, and it was very similar to yours, so hopefully it’s a case of great minds thinking alike and all that!

    Thanks again for your reply, and it’s all working brilliantly now!

    Alex

    $meta_query = array(
    				
    				'relation' => 'OR',
    
    				array(
    					'key' => 'end_date',
    					'compare' => '>=',
    					'value' => $today,
    				),
    				
    				array(
                        'key' => 'event_date',
                        'value' => 'standard',
                        'compare' => 'NOT LIKE',
                    ),
    				
    			);
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Event Date Sorting’ is closed to new replies.