Master Etch with tutorials, tips, and tricks.

Search

Recent articles

Event Query

  • Queries

I started working on another personal project that requires showing upcoming events while using Etch. This assumes you are using a custom post type and custom fields for the start date, but the same should work with any event plugin as long as you can access the post type.

In the examples I am using, a custom post type called “events” with a Meta Box custom datetime field called e_start_date. The default storage format is already sufficient, so no additional manipulation is required.

The Query

To build the query, you need some basic information:

  • The post type: events
  • Comparison date value: $currentDate
  • The start date meta value: e_start_date
  • The start date meta field name: e_start_date
  • Use the STRING type for comparison, not DATE
$query_args = [
  'post_type' => 'events',
  'posts_per_page' => $count ?? 12,
  'post_status' => 'publish',
  'meta_key' => 'e_start_date',
  'orderby' => 'meta_value',
  'order' => 'ASC',
  'meta_query' => [
    [
      'key' => 'e_start_date',
      'value' => $currentDate,
      'compare' => '>=',
      'type' => 'STRING'
    ]
  ]
];

The loop

To set up the loop in Etch, you call the query (e.g., events) and then pass the site’s current date (site.currentDate.dateFormat(‘Y-m-d’)) for the $currentDate argument.

{#loop events($currentDate: site.currentDate.dateFormat('Y-m-d')) as e}

Share Your Thoughts

Your email address will not be published. Required fields are marked *