WordPress.org

Ready to get started?Download WordPress

Forums

Simple Fields
[resolved] Sort WP_Query by Date and Time field, but only include results that haven't past (4 posts)

  1. jklina
    Member
    Posted 1 year ago #

    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/

  2. Pär Thernström
    Member
    Plugin Author

    Posted 1 year ago #

    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

  3. jklina
    Member
    Posted 1 year ago #

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

  4. emilysparkle
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic