• Resolved jklina

    (@jklina)


    In my query I currently have:

    $recent_events = new WP_Query(array('post_type' => 'phelps_event', 'posts_per_page' => 3, 'orderby' => 'meta_value', 'meta_key' => '_simple_fields_fieldGroupID_4_fieldID_1_numInSet_0', 'meta_value' => time(), 'meta_compare' => '>=', 'order' => 'ASC'))

    My goal is to get the closest 3 events to today, including today, but exclude any events that may have passed. I’m currently using the new Date and Time field, but am not sure what to put in as my ‘meta_value’. I suspect it requires a specific time format?

    Thanks for looking!

    http://wordpress.org/extend/plugins/simple-fields/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor eskapism

    (@eskapism)

    hm, you’re doing more advanced stuff here than I’ve done with this new field. However, since it stores the dates in ISO 8601-format (YYYY-MM-DD) and according to wikipedia “The lexicographical order of the representation thus corresponds to chronological order” (http://en.wikipedia.org/wiki/ISO_8601#General_principles) so that means that we should be able to sort them.

    So… Perhaps like this:

    // Retrieve three posts with dates that are from and including today and forward
    $recent_events = new WP_Query(array(
      'post_type' => 'phelps_event',
      'posts_per_page' => 3,
      'orderby' => 'meta_value',
      'meta_key' => '_simple_fields_fieldGroupID_4_fieldID_1_numInSet_0',
      'meta_value' => date("Y-m-d"),
      'meta_compare' => '>=',
    'order' => 'ASC'));

    Let me know if that works for you. If not I’ll try to make an example myself and post in on the blog at simple-fields.com/blog

    Thread Starter jklina

    (@jklina)

    Awesome observation and it works beautifully! Thanks Pär!

    this is really close to what i want to do… i currently use Display Posts Shortcode plugin to show my upcoming events on her home page. what i REALLY want to do is display th next three events (with no past events listed, as above) as they are defined in a date-select calendar field on the editor screens.

    where would i put the code above to get started? can i integrate it into the shortcode plugin?

    i have installed the plugin and gotten to define a date on the upcoming event posts, but can’t figure out how to display or sort them.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sort WP_Query by Date and Time field, but only include results that haven't past’ is closed to new replies.