Title: Show posts based on event date (custom variable)
Last modified: August 20, 2016

---

# Show posts based on event date (custom variable)

 *  [Eckstein](https://wordpress.org/support/users/eckstein/)
 * (@eckstein)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/show-posts-based-on-event-date-custom-variable/)
 * I am developing my first custom theme, my client is a theatre company/venue.
 * On the home page, what I would like to do is show a “rolling” list of the next
   upcoming three shows. Each upcoming show is displayed using a post with an excerpt
   and a featured image. Many shows are posted well in advance of the performance
   dates so there may be 5-10 shows that are posted under the “current season” section
   of the site. This is ideal.
 * However, in order to display the three upcoming events on the homepage automatically
   I would need to somehow tell WordPress to display a maximum of three posts based
   on the date of the performance and NOT based on the date that the post was published.
 * I’m thinking it would be something along the lines of assigning a custom field
   that I insert a date range into for each post and then build some php code that
   checks to see:
    1) If the date range has passed 2) If not, how far away is the
   start of the date range from the current day (new variable) 3) Finally, sort 
   a max of three posts based on that newly created variable in ascending order.
 * I understand exactly that code I need, but am not experienced enough to build
   it myself.
 * Anyone out there have either some code to use or a different way I might be able
   to do that?
 * Thanks!
 * (ps: I do not have a link to include because I am building this theme in a local
   environment on my computer and I’m simply looking for functionality advice.)

Viewing 1 replies (of 1 total)

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/show-posts-based-on-event-date-custom-variable/#post-2911558)
 * I don’t quite understand how you want to handle the sort if the start date has
   passed, but the end date has not.
 * Assuming you have custom fields ‘event-start-date’ and ‘event-end-date’ with 
   values in ‘YYYY-MM-DD’ format, here is a query that will select 3 posts where
   the end date is greater than or equal to the current date and sort in order of
   the start date:
 *     ```
       $args = array(
          'posts_per_page' => 3,
          'meta_key' => 'event-start-date',
          'orderby' => 'meta_value',
          'order' => 'ASC',
          'meta_query' => array(
             array( 'key' => 'event-end-date', 'compare' => '>=', 'value' => date('Y-m-d') )
          )
       );
       query_posts($args);
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Show posts based on event date (custom variable)’ is closed to new replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [13 years, 10 months ago](https://wordpress.org/support/topic/show-posts-based-on-event-date-custom-variable/#post-2911558)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
