WordPress.org

Support

Support » How-To and Troubleshooting » Show posts based on event date (custom variable)

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)
  • 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.
Skip to toolbar