Query posts by custom date field using archive.php
-
Dear all,
I am working on a very simple and straight forward event website. I use the normal posts to create ‘events’ and use the Advanced Custom Field plugin to add two date fields, one start_date (where the event begins) one end_date (the date the event ends) in case the event is only one day long, I use only the end_date field.
The home page should filter all the upcoming posts for the current month (this would be a custom loop in the index using the current date), and has a simple select dropdown menu to select a day, month and year, and a ‘Go’ button
My assumption would be that if the archive.php file can do this trough changing the url like,
website.com/2014 (all the posts written in 2014)
website.com/2014/11 (all the posts written in november of 2014)
website.com/2014/11/26 (all the posts written on the 26th of November 2014)I could have these urls show the posts using the end_date custom field instead of the post publish date with the addition of showing the posts on days between the start_date and end_date.
@robbiegod has posted a very similar question here.
His question is more focused on getting the multiple day events span all the archive pages (not just the day the event ends but all in between), which is something I would like to integrate as well. But how to generally set this up is unclear to me.My current archive.php file
<?php add_filter( 'get_meta_sql', 'get_meta_sql_date', 10, 2 ); $args = array( 'post_type' => 'post', 'posts_per_page' => 10, 'meta_key' => 'end_date', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'start_date', 'compare' => '>=', 'value' => $start_date, 'type' => 'DATE' ), array( 'key' => 'end_date', 'compare' => '<=', 'value' => $end_date, 'type' => 'DATE' ) ), ); $posts = new WP_Query( $args ); remove_filter( 'get_meta_sql', 'get_meta_sql_date', 10, 2 ); if( $posts->have_posts() ) : while ( $posts->have_posts() ) : $posts->the_post(); ?>
and the function that goes with it from @keesiemeijer
// sortable posts by custom date field for long period events function get_meta_sql_date( $pieces, $queries ) { global $wpdb; // get start and end date from query foreach ( $queries as $q ) { if ( !isset( $q['key'] ) ) { return $pieces; } if ( 'start_date' === $q['key'] ) { $start_date = isset( $q['value'] ) ? $q['value'] : ''; } if ( 'end_date' === $q['key'] ) { $end_date = isset( $q['value'] ) ? $q['value'] : ''; } } if ( ( '' === $start_date ) || ( '' === $end_date ) ) { return $pieces; } $query = ""; // after start date AND before end date $_query = " AND ( ( $wpdb->postmeta.meta_key = 'start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s) ) AND ( mt1.meta_key = 'end_date' AND ( CAST(mt1.meta_value AS DATE) <= %s) ) )"; $query .= $wpdb->prepare( $_query, $start_date, $end_date ); // OR before start date AND after end end date $_query = " OR ( ( $wpdb->postmeta.meta_key = 'start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) ) AND ( mt1.meta_key = 'end_date' AND ( CAST(mt1.meta_value AS DATE) >= %s) ) )"; $query .= $wpdb->prepare( $_query, $start_date, $end_date ); // OR before start date AND (before end date AND end date after start date) $_query = " OR ( ( $wpdb->postmeta.meta_key = 'start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s) ) AND ( mt1.meta_key = 'end_date' AND ( CAST(mt1.meta_value AS DATE) <= %s ) AND ( CAST(mt1.meta_value AS DATE) >= %s ) ) )"; $query .= $wpdb->prepare( $_query, $start_date, $end_date, $start_date ); // OR after end date AND (after start date AND start date before end date) ) $_query = "OR ( ( mt1.meta_key = 'end_date' AND ( CAST(mt1.meta_value AS DATE) >= %s ) ) AND ( $wpdb->postmeta.meta_key = 'start_date' AND ( CAST($wpdb->postmeta.meta_value AS DATE) >= %s ) AND ( CAST($wpdb->postmeta.meta_value AS DATE) <= %s ) ) )"; $query .= $wpdb->prepare( $_query, $end_date, $start_date, $end_date ); $pieces['where'] = $query; return $pieces; }
Anny point in the right direction would be very much appreciated.
You can find my test site here.
- The topic ‘Query posts by custom date field using archive.php’ is closed to new replies.