I created a custom post type called Events, and I use advanced custom fields (Wordpress plugin) to add custom fields to posts. One custom field is the date of the event, and my goal is to request messages based on that date (which are stored in the database as "yymmdd") and only show future events. I came up, but I canβt figure out how to integrate the filter with the code that I already wrote.
I know that Wordpress Codex contains information about this here ( http://codex.wordpress.org/Class_Reference/WP_Query ), but as a newbie with PHP, I am at a loss as to how to make it work with my field data. Here's the wordpress code:
// Create a new filtering function that will add our where clause to the query function filter_where( $where = '' ) { // posts for March 1 to March 15, 2010 $where .= " AND post_date >= '2010-03-01' AND post_date < '2010-03-16'"; return $where; } add_filter( 'posts_where', 'filter_where' ); $query = new WP_Query( $query_string ); remove_filter( 'posts_where', 'filter_where' );
But I'm not sure how to integrate this with my current request, or how to use my date field as the value of the request, not the publication date. Below I write so far.
<?php $args = array( 'post_type' => 'events', 'posts_per_page' => 4, 'meta_key' => 'event_date', 'orderby' => 'meta_value_num', 'order' => 'ASC' ); $loop = new WP_Query( $args ); if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?> <h1><?php the_title()</h1> <?php $eventdate = date_create(get_field('event_date')); ?> <small><?php echo date_format($eventdate,'M d'); ?></small> <?php endwhile; endif; ?>
I would really appreciate it if someone could show me how to add a filter to my query and how to use the custom field meta-field for the date of the event, and not the date of publication. I seem to have given all the necessary information, but let me know if I missed something. Thanks!
source share