How to query and order posts with meta field 'event-date' > some date? (3 posts)

  1. richcon
    Posted 5 years ago #

    I'm making a list display of 'events' (using custom post types), and there's meta fields for beginning date and ending date (each storing the date as an integer usable with PHP's date() function). I want to show current and upcoming events, so events whose 'end date' meta field is greater than or equal to the current date.

    I also want to order events by start date, so how can I order by a meta field?

    Thanks to anyone who can point me in the right direction!

  2. esmi
    Forum Moderator
    Posted 5 years ago #

  3. Peter Hardy-vanDoorn
    Posted 5 years ago #

    Here's how I do it with a modified WP_Query:

    <?php $args = array(	'post_type'	=> 'events',
    			'post_status'	=> 'publish',
    			'order'		=> 'ASC',
    			'orderby'	=> 'meta_value',
    			'meta_key' 	=> 'event-date-start' ); ?>
    <?php $eventslist = new WP_Query(); ?>
    <?php $eventslist->query( $args ); ?>

    The important bit is meta_key which tells it to look at the custom field called 'event-date-start' (in my case, yours will no doubt be different!)

    Hope that helps


Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.