• Hi, any help here would be amazing I’m going a bit mad!

    I am trying to create custom post meta date fields that can display a start and end time for an event attached to a post. I do not want a custom post type, just simple meta boxes that then display this information on the front end if applicable.

    My problem is on the front end:

    When displaying the dates I need the start date to sort by newest first and the no dates that have passed (EG older than the current day) to display either.

    I’ve been folowng this tutorial: http://wptheming.com/2011/11/event-posts-in-wordpress/

    But is stops short of telling me how to get this query working as I described above using the last piece of code:

    `
    $args = array( ‘post_type’ => ‘event’,
    ‘meta_key’ => ‘_start_eventtimestamp’,
    ‘orderby’=> ‘meta_value_num’,
    ‘order’ => ‘ASC’,
    ‘posts_per_page’ => 20,
    );
    $events = new WP_Query( $args );

    if ( $events->have_posts() ) :
    echo ‘<ul>’;
    while ( $events->have_posts() ) : $events->the_post();
    echo ‘<li><a href=”‘ . get_permalink() . ‘”>’ . get_the_title() . ‘</a></li>’;
    endwhile;
    echo ‘</ul>’;
    endif;
    `

    I am totally stuck!

Viewing 1 replies (of 1 total)
  • Thread Starter matt-tyas

    (@matt-tyas)

    The actual code I am using in my template is here, it sorts by date entered (in the custom meta box, but still shows events from the past.

    <?php
    	$args = array(
    		'category_name' => 'featured',
    		'meta_key' => '_start_eventtimestamp',
    		'orderby'=> 'meta_value_num',
    		'order' => 'ASC',
    		'posts_per_page' => 4,
    		 );
    		$events = new WP_Query( $args );
    
    		if ( $events->have_posts() ) :
    			echo '<article class="event media">';
    
    			while ( $events->have_posts() ) : $events->the_post();
    			echo '<a href="' . get_permalink() . '">';
    
    			echo '<div class="small-thumb media__img">';
    			echo get_the_post_thumbnail($post_id, array( 120,120 ));
    			echo '</div>';
    
    			echo '<p class="event-time">';
    
    			echo '' . get_post_meta($post->ID, '_start_day', true) . '/';
    			echo '' . get_post_meta($post->ID, '_start_month', true) . '/';
    			echo '' . get_post_meta($post->ID, '_start_year', true) . ' ';
    			echo '' . get_post_meta($post->ID, '_start_hour', true) . ':';
    			echo '' . get_post_meta($post->ID, '_start_minute', true);
                            echo ' – ' . get_post_meta($post->ID, '_end_day', true) . '/';
    			echo '' . get_post_meta($post->ID, '_end_month', true) . '/';
    			echo '' . get_post_meta($post->ID, '_end_year', true) . ' ';
    			echo '' . get_post_meta($post->ID, '_end_hour', true) . ':';
    			echo '' . get_post_meta($post->ID, '_end_minute', true);
    
    			echo '</p>';
    
    			echo '<h5>' . get_the_title() . '</h5>';
    
    			echo '</a>';
    
    			echo '</article>';
    		endwhile;
    
    	endif;
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Post meta date sorting by event date and present day’ is closed to new replies.