0

I'm writing a function to find the users next upcoming event (stored as CPT), chronologically.

If I use a normal query and do something like

'post_type' => 'ticket', // another CPT set up to act as ticket 
'orderby'   => 'meta_value_num',
'meta_key'  => 'event_date', // currently just Ymd but can be changed to unix / whatever if neccesary.
'order'     => 'ASC',
'posts_per_page' => 1, // changing to -1 to get all tickets 
'meta_query' => array(
    array(
        'key'   => 'member',
        'value' => $user_id //user ID stored as meta on ticket CPT
    ),
    array(
         'key'   => 'state',
         'value' => 'complete' // to make sure is a confirmed & paid ticket
    )
)

That will find the ticket post type that is the last chronologically in the list ie. the "most future" event.

What I'd love to do then is to put a filter through the results to remove all the tickets that are before today ( I've previously used if $event_date >= date("Ymd") )

Then from those results use an opposite or a separate orderby to get the first upcoming event.

Please comment if you need any more information. Happy to pastebin any extra code you feel you need.

  • 1
    You can use the date_query for this in a similar way to the meta_query - codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters. You may also need to change the order to 'DESC' so that your closest event is found. – David Gard Jan 28 '15 at 15:31
  • Hang on, maybe ignore my last comment... I was thinking of post_date. How are you storing the date of your event_date metakey? If it is as a MySQL timedate or datestamp field, you can use the posts_where filter to add a condition that ensures only event dates in the future are found. – David Gard Jan 28 '15 at 15:43
  • Hi @DavidGard - thanks for your input. The date is stored as post meta on the Custom Post Type of 'Ticket'. At the moment the format I'm using is simply "Ymd" but I can change that to any format that helps. I will look up the posts_where filter you talk of as that sounds like it could be what I'm looking for. – Bysander Jan 28 '15 at 17:00
  • Possibly another solution? Could I put a meta query in a range of numbers... IE if meta event_date is in the range of date("Ymd") to 99999999 ??? – Bysander Jan 28 '15 at 17:20
  • EDIT: just found this page Which may help me. Unfortunately I have to leave it until tomorrow but will see how I get on and if successful will post answer – Bysander Jan 28 '15 at 17:29
0

thanks for responses,

Allowed me to find correct answer which I've posted below -

$tickets_for_user = get_posts(array(
                                        'post_type' => 'ticket',
                                        'orderby' => 'meta_value_num',
                                        'meta_key' => 'event_date',
                                        'order' => 'ASC',
                                        'posts_per_page' => 1,
                                        'meta_query' => array(
                                                              'relation' => 'AND',
                                                              array(
                                                                    'key' => 'event_date',
                                                                    'value' => array($today,'20991231'),
                                                                    'compare' => 'BETWEEN',
                                                                    'type' => 'DATE',
                                                                    ),
                                                              array(
                                                                    'key' => 'member',
                                                                    'value' => $user_id
                                                                    ),
                                                              array(
                                                                    'key' => 'state',
                                                                    'value' => 'complete'
                                                                    ),
                                                              )
                                        ));

    $tickets = array();
    foreach($tickets_for_user as $ticket) {

Important note from the Codex:

The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYYMMDD and tested with this format.

You will also have to put the dates in order, with the earliest coming first in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.