Show current WordPress posts between “start” and “end” date custom fields

So I am trying to show only current events that fall between the start date (st_date) and the end date (end_date) custom fields. Currently this is not working at all for the current events.

My current time on the WordPress install is correct as per my timezone (eastern) at -5.

Read More

I have tried to change the date format on my WordPress site to match Y-m-d, but since the upcoming and previous events are showing up, it obviously didn’t help me out.

Is it the server time that it’s working off of? As of right now on 12/31, it is showing an event with a start date of 12/30, but not the event I created with a start date of 12/31.

Here is my current code. Any ideas on what needs to changed? Thanks for any help!

    //FILER FOR SEPARATING UPCOMING, CURRENT AND PAST EVENTS
function event_where($where)
{
    global $wpdb,$wp_query;
    $current_term = $wp_query->get_queried_object();
    if((is_archive() || is_tag()) && ($current_term->taxonomy==CUSTOM_CATEGORY_TYPE1 || $current_term->taxonomy==CUSTOM_TAG_TYPE1))
    {
        if($current_term->taxonomy == CUSTOM_CATEGORY_TYPE1 || $current_term->taxonomy == CUSTOM_TAG_TYPE1)
        {
            if(@$_REQUEST['etype']=='')
            {
                $_REQUEST['etype']='current';
            }
            if(@$_REQUEST['etype']=='current')
            {
                $today = date('Y-m-d G:i:s');
                $where .= "  AND ($wpdb->posts.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='st_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') <='".$today."')) AND ($wpdb->posts.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='end_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') > '".$today."')) ";
            }
            elseif($_REQUEST['etype']=='upcoming')
            {
                $today = date('Y-m-d G:i:s');
                $where .= " AND ($wpdb->posts.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='st_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') >'".$today."' and $wpdb->posts.post_status = 'publish')) ";
            }
            elseif($_REQUEST['etype']=='past')
            {
                $today = date('Y-m-d G:i:s');
                $where .= " AND ($wpdb->posts.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='end_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') < '".$today."')) ";
            }
        }elseif(is_day() || is_month() || is_year())
        {
            $where = str_replace("'post'","'".CUSTOM_POST_TYPE1."'",$where); 
        }
    }
    return $where;
}

Related posts

Leave a Reply

1 comment

  1. A few things I can think of to check:

    If you’re concerned about time zones being incorrect, you can always have MySQL supply the datetime. You would change a statement like this:

    $today = date('Y-m-d G:i:s');
    $where = "AND date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') <='" . $today . "'))";
    

    to something like this:

    $where = "AND date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') <= NOW() ))";
    

    NOW() is MySQL’s command to give the current server datetime.

    When you’re entering the start and end dates into the custom fields, are you using the date or the date and time? If you’re not using a time, a date such as “2012-12-31” would get interpreted as “2012-12-31 00:00:00”. That could cause problems, especially with an end date. Consider adding a day (or 86,400 seconds) to the end date of this is the case.

    If you suspect you’re having problems interpreting dates properly, MySQL has an equivalent to PHP’s strtotime() command: STR_TO_DATE. You can take a human readable string and convert it to exactly the format you want.

    When using $wpdb for queries, you can try outputting $wpdb->last_query to see exactly when the full SQL sent to MySQL is. Try running that in your favorite MySQL client and see what results you get back.