[Support request] Ordering Events by Custom Field Date

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Ordering Events by Custom Field Date

This topic contains 1 reply, has 2 voices, and was last updated by  Tom 6 months, 2 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #9583

    Heidi
    Participant

    Hi All,

    I’m using WP Show Posts and ACF for a custom post type of EVENTS and want to sort them by the custom date field of event_date (Date Picker) and display only current events. I’m struggling with the PHP, but here’s the events page and the code I’m using. I’m sure I’ve got the code wrong. Any help appreciated!

    if ( 2506 == $settings[ 'list_id' ] ) {   // beginning of events list	 
    	$meta_events = get_post_meta( get_the_ID(), 'short_description', true );
    	$date = get_field('event_date', false, false);	
    	$date = new DateTime($date);
    	$time_start = get_field('event_start_time', false, false);
    	$time_start = new DateTime($time_start);
    	$time_end = get_field('event_end_time', false, false);
    	$time_end = new DateTime($time_end);
    	$today = date('Ymd');
        
    $event_args = array (
        'post_type' => 'events',
    	'meta_key'	=> 'event_date',
    	'orderby'	=> 'meta_value_num',
    	'order'		=> 'ASC',
        'meta_query' => array(
    		array(
    	        'key'		=> 'event_date',
    	        'compare'	=> '<=',
    	        'value'		=> $today,
    	    ),
    			
    ),
    );
    	   
    $posts_array = get_posts($event_args);
    	   
    	    foreach($posts_array as $post)
      {
            echo '<div class="films-page-syn">' . $meta_events . '</div>';	
    		echo '<div class="event-date">';
    			echo $date->format('F j, Y'), " // ", $time_start->format('g:i a'), "-", $time_end->format('g:i a');
    		echo '</div>';	
        }	
    }	// end of events list
    #9591

    Tom
    Keymaster

    Hi there,

    That code looks like you’re creating your own loop instead of using WP Show Posts?

    I kind of showed how to alter the WPSP query here: https://wpshowposts.com/support/topic/show-newest-latest-products-each-week/#post-9551

    Let me know if you need more info 🙂

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.