WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need help caluclating future date in the Loop using custom field (6 posts)

  1. TrishaM
    Member
    Posted 5 years ago #

    Need to create page of future events - have tried about every events calendar style plugin available and none work as I need (not enough fields) SO I'm going to try my hand at using custom fields (no problems there) and creating a specific template page for a singe "events" category (no problem there either).

    Where I need help is with calculating which events to display based on an "event date" not the publish date of the post UNLESS there is a way to use the Publish date AS the event date and still display it before the publish date.

    This is for a non-profit wilderness organization that wants to display a list of upcoming hikes and exclude hikes that have already occurred. Since I can't get any of the special plugins to do this and still have enough fields for all hike details, I'm going to use Posts with custom fields for the hike details and the post body for the hike description.

    So HOW do I write The Loop so that it looks at the Custom Field for Hike Date, not the post publish date, to determine if the page should display the post?

    Many thanks for any help any of you experienced PHP coders can lend - I've been racking my brains on this one for many months and have finally thrown in the towel and decided to go this route - I've only put it off and continued to try various plugins because of this future-date issue and my lack of PHP knowledge to make the right Loop statement.

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    Could you use query_posts with custom field parameters?

    http://codex.wordpress.org/Template_Tags/query_posts#Custom_Field_Parameters

    No idea if this will work but what about:

    <?php
    $today = date('Y-m-d');
    query_posts('meta_key=hike_date&meta_value >=.'$today.'&orderby=meta_value');
    ?>
  3. TrishaM
    Member
    Posted 5 years ago #

    This was definitely the right way to go......

    The code pasted above resulted in an error, but I found & fixed it quickly - the part " .'$today.' just needed the first period to be moved to inside the quote....but it still didn't work, so I looked closer at the examples on the page you linked to, and made a few minor adjustments so now it looks like this:

    `<?php
    $today = date('m-d-y');
    query_posts('cat=7&meta_key=hike_date&meta_compare=>=&meta_value='.$today.'&orderby=meta_value');
    ?>
    ~

    NOW it seems to work perfectly!

    I cannot thank you enough - have been beating myself up over this for way too long - so foolish - I should have known that someone in this forum could help - thank you SO MUCH!

  4. esmi
    Forum Moderator
    Posted 4 years ago #

    Sorry for the syntax error. That was just plain clumsy on my part. Glad to hear that it worked in the end, though. I've been thinking of developing a similar template approach to event promotion myself. So this will help me too. :-)

  5. karl.wills
    Member
    Posted 4 years ago #

    I am looking to do the same thing but am unsure where to place the query and what format should the dat be in the custom field?

    Thanks

  6. ricardormz
    Member
    Posted 4 years ago #

    There is a problem with:
    meta_key=date&orderby=meta_value

    If your meta_value date format is mm-dd-yyyy, will sort like this:
    (01) January 2010
    (02) february 2010
    (10) october 2009
    (11) november 2009
    (12) december 2009

    Instead of:
    (10) october 2009
    (11) november 2009
    (12) december 2009
    (01) January 2010
    (02) february 2010

    Guess what this is really sorting? I think its simply because is not an actual date, but a string.

    Unfortunatelly this:
    orderby=date(meta_value)
    does not work.

    I can't use custom select query because this brakes a couple of plugins im using.

    Any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.