Support » Themes and Templates » How to query and order posts with meta field 'event-date' > some date?

  • 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!
    Richard

Viewing 2 replies - 1 through 2 (of 2 total)
  • esmi

    (@esmi)

    Forum Moderator

    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

    Peter

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to query and order posts with meta field 'event-date' > some date?’ is closed to new replies.