• I hope someone can help. I’ve read loads of posts but can;t work this out…

    I have an ‘events’ post type which allows entry of a start and end date.
    1. I want to list items by the start date value (easy – I am using the built in query_posts at the moment)

    2. I only want to show posts where the end date is in the future (I can also do this with query_posts – using a meta array).

    However, when the event is a one day event the user be won’t filling in the end date so there will be no value in the database.

    Currently I have been querying the posts by order of start date and then once in The Loop I’ve assigned my meta values to variables and used IF’s to decide whether to use the start or end date value against today’s date:

    `// if there IS an end date value then $event_date_end is used
    if ($event_date_end[‘data’]) $event_ed = $event_date_end;

    // if there IS NOT an end date value then $event_start_date is used
    if (!$event_date_end[‘data’]) $event_ed = $event_date_start;

    // converts $event_ed to format YYYYMMDD
    $event_ed = date(‘Ymd’, strtotime($event_ed));

    //gets current date in format YYYYMMD
    $today = date(‘Ymd’); D

    // if $event_ed is in the future (including today) then do the following…
    if ( $event_ed >= $today ) {

    …THE POST… }

    Basically I want to do what the above code is doing at the query stage. Partly becasue it’s tidier but also I can then choose how many posts to display – currently I have to display all posts for the IF’s in the loop so be effective

Viewing 1 replies (of 1 total)
  • Thread Starter wambrose

    (@wambrose)

    Ok, so having thought about this query for a while I think I can simplify it to this:

    GET POSTS:
    ordered by meta key: START DATE
    AND meta key: START DATE >= TODAY
    OR meta key: END DATE >= TODAY

    btw: There is always a start date but I want the ‘OR’ part for cases where the start date has passed (< TODAY) but the end date is still in the future.

    I just need help turning it into a custom select query.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom select query (IF meta_key has value….)’ is closed to new replies.