WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Ordering by Event Date (11 posts)

  1. Daffydd57
    Member
    Posted 3 years ago #

    Hi,

    I'm setting up a category for "Coming Events" - I want to be able to order by custom field similar to the solution found
    in this thread.

    Using this as the query:
    query_posts('cat=3&order=desc&orderby=meta_value&meta_key=event_date');

    and in the custom field

    Name:
    event_date

    Value:
    2010-01-12

    It works until I add an event for the next year. In other words the event fall into place like this:

    Rusk Co. PRCA Rodeo – Henderson, TX
    Event Date: Jan. 12, 2011

    Rusk Co. PRCA Rodeo – Henderson, TX
    Event Date: October 15, 2010

    Rusk Co. PRCA Rodeo – Henderson, TX
    Event Date: October 16, 2010

    The first entry (dated for Jan. 12, 2011) should be below the others. In the Value field I have 2011-01-12

    What am I doing incorrectly?

    Thanks!
    Daf

  2. Daffydd57
    Member
    Posted 3 years ago #

    I think I may have solved this. Reference this page:
    http://matth.eu/wordpress-date-field-plugin

    I used the code he suggests:

    query_posts('cat=3meta_key=event_date_value&orderby=meta_value&meta_compare=>=&meta_value=' . time() . '&order=ASC' );

    and it appears to be working correctly.

    Thanks!

  3. Daffydd57
    Member
    Posted 3 years ago #

    Scratch that - it does help by putting the years in order but the months and days fall where ever.

    Any ideas on how to get this to work?

    Thanks!

  4. vtxyzzy
    Member
    Posted 3 years ago #

    In your original post, you used order=desc. That seems backward to me. Have you tried the original query with order=ASC?

  5. Daffydd57
    Member
    Posted 3 years ago #

    Yes - it needs to be desc in order for the events to fall soonest first. Thanks

  6. vtxyzzy
    Member
    Posted 3 years ago #

    desc will put the greatest date first, so 2011 will come before 2010.

    I think the plugin is what you need. Don't know why it isn't working.

  7. vtxyzzy
    Member
    Posted 3 years ago #

    If you don't want to use the plugin, I think this will work for you. It will get all category 3 posts with a meta_key of event_date where the date (stored as 'yyyy-mm-dd') is greater than or equal to today. The posts will be in the order of today, tomorrow, ... future.

    $args = array(
          'cat' = 3,
         'meta_key' => 'event_date',
         'orderby' => 'meta_value',
         'order' => 'ASC',
         'meta_compare' => '>=',
         'meta_value' => date_i18n('Y-m-d'),
         'caller_get_posts' => 1,
                     );
    query_posts($args);

    If you want to display the event date, add this just ahead of the code above:

    function mam_posts_fields ($fields) {
       global $mam_global_fields;
       if ($mam_global_fields) $fields .= (preg_match('/^(\s+)?,/',$mam_global_fields)) ? $mam_global_fields : ", $mam_global_fields";
       return $fields;
    }
    add_filter('posts_fields','mam_posts_fields');
    $mam_global_fields = ', meta_value as event_date';

    Then, you can get the event date with $post->event_date.

  8. Daffydd57
    Member
    Posted 3 years ago #

    Hi vtxyzzy,

    Thanks! I have to asked tho - where exactly does that code go? Within the loop? Should it be within php tags? I don't know much about php, just kind of winging it with examples and stuff I find. ;)

    I'm using the loop on an external site - using WP as a CMS so everything is loop based.

    Thanks again!

  9. Daffydd57
    Member
    Posted 3 years ago #

    I figured it out - it replaces my existing query post statement - :)

    This appears to be working correctly - Let me continue to test it today and I will get back with you. Thank you so much for helping! :)

  10. Daffydd57
    Member
    Posted 3 years ago #

    One other note for anyone else who may come by and use the code above -

    'cat' = 3,

    should, I think be

    'cat' => 3,

    I got an "expecting error" until I changed that. :)

    Another note - I had a little trouble when I tried to use the same code - different cat id - for the "Past Events" page -nothing would show up! Arg! But I finally figured out that you have to change:

    'meta_compare' => '>=',

    to

    'meta_compare' => '<=',

    so that the OLDER articles met the conditions.

    Thanks again!!

  11. vtxyzzy
    Member
    Posted 3 years ago #

    You are welcome. Now, please use the dropdown at top right to mark this topic 'Resolved'.

Topic Closed

This topic has been closed to new replies.

About this Topic