0

I currently have a custom post type called 'events'. I created this from a tutorial here http://tatiyants.com/how-to-use-wordpress-custom-post-types-to-add-events-to-your-site/. I want to query the date to only show the posts with dates coming up and not ones on the past. $event_date >= time

However in the tutorial he displays the results using a shortcode. I am trying to convert this to a standard wp loop so I can display it in my index.php. He uses the following within his shortcode function:

add_shortcode( 'events', 'get_events_shortcode' );

function get_events_shortcode($atts){
    global $post;

    // get shortcode parameter for daterange (can be "current" or "past")
    extract( shortcode_atts( array(
        'daterange' => 'current',
    ), $atts ) );

    ob_start();

    // prepare to get a list of events sorted by the event date
    $args = array(
        'post_type' => 'events',
        'orderby'   => 'event_date',
        'meta_key'  => 'event_date',
        'order'     => 'ASC'
    );

    query_posts( $args );

    $events_found = false;

    // build up the HTML from the retrieved list of events
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post();
            $event_date = get_post_meta($post->ID, 'event_date', true);

            switch ($daterange) {
                case "current":
                    if ($event_date >= time() ) {
                        echo get_event_container();
                        $events_found = true;
                    }
                    break;

                case "past":
                    if ($event_date < time() ) {
                        echo get_past_event_summary();
                        $events_found = true;
                    }
                    break;
            }
        }
    }

    wp_reset_query();

    if (!$events_found) {
        echo "<p>no events found.</p>";
    }

    $output_string = ob_get_contents();
    ob_end_clean();

    return $output_string;
}

Any help or direction will be greatly appreciated as though I have programmed themes before this is causing me a headache.

2 Answers 2

2

You want to query for posts with event_date in the future but you have saved (or so it seems from a comment), your dates as:

date("F", $unixtime)." ".date("d", $unixtime).", ".date("Y", $unixtime);

That is "textual month name" + "numeric day" + "numeric year". There is no way to query/sort that format so that it matches a calendar. Your sorting options are "alphabetical", which is obviously going to be wrong, or "numerical", which is also obviously wrong. The only human readable date format that sorts in a way that matches a calendar is "numeric year" + "numeric month" + "numeric day", or "YYYY-MM-DD". The choice of separator is arbitrary and can be nothing. That is date('Y-m-d'). If your dates were stored in a rational format a query like the following should do what you need:

$args = array(
    'post_type' => 'events',
    'orderby'   => 'event_date',
    'meta_key'  => 'event_date',
    'order'     => 'ASC',
    'meta_query' => array(
      array(
        'key' => 'event_date',
        'value' => date('Y-m-d',time()),
        'compare' => '>'
      )
    ),
);
$q = new WP_Query($args);
var_dump($q->request);

You can always just store a Unix timestamp as well. Those sort matching calendar order as well. You would then format the date on display. This is my preference with dates unless you are saving to "date" format column in a MySQL table. If you save as a timestamp, use:

'value' => strtotime('today'),

instead of:

'value' => date('Y-m-d',time()),
2
  • hi @s_ha_dum . I am converting the event_date to UNIX time stamp using if(isset($_POST["event_date"])) { update_post_meta($post->ID, "event_date", strtotime($_POST["event_date"])); I am wantin to query inside the loop. What am I doing with the code you suggested? }` Nov 22, 2013 at 17:17
  • I don't understand the question. Once you've converted the dates the code should work, just pass a timestamp instead of date('Y-m-d',time()),. I'll update and add the right line.
    – s_ha_dum
    Nov 22, 2013 at 17:31
0

This will query / show all future posts - standard WP Query surrounded by 2 filters, 1 to add the filter so it applies to this query and another to remove it so it doesn't apply to any other queries:

This would be the query for your custom loop:

add_filter( 'posts_where', 'filter_where' );
$events = new WP_Query(array('post_type' => 'post', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'date', 'post_status' => array('publish', 'future')));
remove_filter( 'posts_where', 'filter_where' );

Add this to your functions file for the filter:

function filter_where( $where = '' ) {
    global $wpdb;
    $where .= " AND ($wpdb->postmeta.meta_key = 'event_date' AND $wpdb->postmeta.meta_value >= '".date('Y-m-d')."')";
    return $where;
}

These 2 combined will query all posts on todays days and after, then order them by date. Is this what you're referring to?

7
  • looks like it. However it doesn't show anything when i apply it :/ Nov 22, 2013 at 15:35
  • Try changing post_type => 'post' to post_type => 'events' You might want to read WP_Query Codex to have 100% full customization of it.
    – Howdy_McGee
    Nov 22, 2013 at 15:39
  • I had already done that. Also changed post_date to event_date which holds the date but it still fails to work. I completely understand what you are trying to do but I don't know why it isn't working. Nov 22, 2013 at 15:50
  • the date is formated in... return date("F", $unixtime)." ".date("d", $unixtime).", ".date("Y", $unixtime); Not sure if that would be a issue. Nov 22, 2013 at 15:53
  • 1
    It's better not to accept my answer so other people will be encouraged to come in and help. Thanks though and good luck!
    – Howdy_McGee
    Nov 22, 2013 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.