• Resolved Daffydd57

    (@daffydd57)


    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

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter Daffydd57

    (@daffydd57)

    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!

    Thread Starter Daffydd57

    (@daffydd57)

    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!

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

    Thread Starter Daffydd57

    (@daffydd57)

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

    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.

    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.

    Thread Starter Daffydd57

    (@daffydd57)

    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!

    Thread Starter Daffydd57

    (@daffydd57)

    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! 🙂

    Thread Starter Daffydd57

    (@daffydd57)

    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!!

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

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Ordering by Event Date’ is closed to new replies.