I run an events company whose website uses a custom post type system for posts (blog), events and tickets all stored as CPT's in the DB.

On the guestlist (which counts 'ticket' custom posts if the event [post] number == current post) I would like to implement a "New Customer" tag to prompt the host to look out for them.

There are two problems with this: you cannot search for the oldest 'ticket' post, as they may not have bought them in chronological order, and you cannot get all tickets and order by post number as they may not have been added to the site in chronological order either.

The two ways I can think of getting around this is either:

(1) By multiple search arrays, i.e. query TICKET > to get USER > get all TICKETS for USER NUMBER > get EVENT [POST] NUMBERS from those tickets (stored as meta) > get EVENTS by post number > - SORT ARRAY BY DATE of event > IF FIRST EVENT IN THAT ARRAY == CURRENT EVENT [post] NUMBER echo NEW CUSTOMER
This seems to be an awfully long way around and I am a little reluctant to try and make all that work as a relatively novice coder.

(2) Somehow retrospectively adding a 'date of event' meta data tag to all ticket posts and then I can directly query them.... Much easier, but I am unsure how to then re-translate that 010214 so that the query recognises it as a date rather than a number...

Number (2) would be ideal but I can't find any information as to how to add all this data retrospectively, and then for future posts - all the data is there in the system but all searches for information lead me to people talking about going back and batch adding search engine meta, not WordPress post meta.

I started coding Number (1) and realised the mountain ahead of me and stopped - if anyone would like that portion of the code though, I can still post it...

Thanks in advance, sorry for the long question - trying to equip any helpers with as much information as possible.

EDIT: A way around this could be actually to use the event page itself as each $event and $ticket are called there.

Structure could be:

write meta to `$ticket`
meta key = 'event_date'
value = $eventDay [= date( 'dmy', $event->start );]

Then a simple query to find all user tickets, and order by this new meta tag 'event_date'.

If first entry event number == current post, BINGO!

  • It is relatively straightforward to query the ticket posts and run through a loop to add some post meta, but I don't know where the date information would come from. Could you tell me more about how the event and ticket are linked? Commented Mar 18, 2014 at 14:11
  • @helgatheviking The event and tickets are only linked through a meta data entry in the 'Ticket' CPTs... The meta key is 'event' the value would be the post number. I use the All-In-One-Event Calendar plug in to manage the events. Once in the event loop you can call the date using $start_time = $event->start_time;
    – Bysander
    Commented Mar 18, 2014 at 14:19
  • Apologies... $EventStartDate = date( 'dmy', $event->start ); gets the date - my last simply generated a time
    – Bysander
    Commented Mar 18, 2014 at 14:25
  • I'm not familiar enough with that plugin to know what the event loop is, but I presume the event date is stored as meta for the event post? Additionally the Posts to Posts plugin might be useful here for creating a link between Tickets and Events. Commented Mar 18, 2014 at 14:33
  • On the face of it, it behaves exactly the same as a post (just more hidden post meta behind it to fill the html out.) Once you're in the event you can call most items with their coded functions (ie the one above). However I think you have to be on that particular page to use them - a bit like wordpress functions like the_time('M j, Y'); relies on you being on that particular post or [I'm told it's] 'being in the loop'
    – Bysander
    Commented Mar 18, 2014 at 14:40

2 Answers 2


This will query all the ticket posts and update a _ticket_date field. However, I'm not yet sure how to retrieve the actual date, so you'll have to provide me with a little more information for that. Assuming it works properly I would run the following once, and then remove. You could achieve a similar effect by using setting a transient but I won't get into that now.

function update_ticket_meta(){

  $args = array( 'post_type' => 'ticket', 'nopagination' => true );
  $tickets = new WP_Query( $args );

  if ( $tickets->have_posts() ) while( $tickets->have_posts() ){

    // get the event ID- adjust meta key as needed
    $event_id = get_post_meta( $post->ID, '_event', true );

    // get the date from the event post
    $event = Ai1ec_Events_Helper::get_event($event_id);

    if( $event ) {
       // get date in desired format- YYYYMMDD should be the easiest to query by
       $date = date( 'ymd', $event->start );

       if( $date )
        update_post_meta( $post->ID, '_ticket_date', $date );

add_action( 'admin_init', 'update_ticket_meta' );

Additionally, to save the meta whenever a ticket is saved you can add an action to save_post-{post-type}. This will still leave you some coding to do if you change the event's date meta field.

// save some meta when a ticket is saved
function add_ticket_date( $post_id ){

    if( !class_exists( 'Ai1ec_Events_Helper' ) )

    // get the event ID- adjust meta key as needed
    $event_id = isset( $_POST['_event'] ) ? (int) $_POST['_event'] : -1;

    // get the date from the event post- again adjust meta key
    $event = Ai1ec_Events_Helper::get_event($post->ID);

    // get date in desired format- YYYYMMDD should be the easiest to query by
    $date = date( 'ymd', $event->start );

    if( $date )
        update_post_meta( $post_id, '_ticket_date', $date );

add_action( 'save_post-ticket', 'add_ticket_date' );
  • Hi @helgatheviking - Unfortunately this doesn't seem to work and throws up a few errors (pastebin.com/FBqfHHDr) - it seems to be related to the events helper so, to be honest, if you do not know the plugin that well I understand if you can go no further... But thank you very much for your help so far!
    – Bysander
    Commented Mar 18, 2014 at 17:15
  • It looks like it is trying to get the event with a null ID? Maybe we aren't sending the right value? Definitely need to change the new $event to use $event_id, but you need to adjust $event_id to be correct as I don't have any idea what that meta key is for you. Commented Mar 18, 2014 at 17:34
  • The meta key on the ticket reads as just 'event' so thought it should work... Let me double check here - '_event' should be the key right $event_id is just a variable - you can call it anything...
    – Bysander
    Commented Mar 19, 2014 at 11:30
  • Hey @helgatheviking - I've had an idea that could be very simple and leave out the entire ai1ec_helper system.... but I think it could need some ironing out... Probably best to do it in chat if you're up for it??
    – Bysander
    Commented Mar 19, 2014 at 12:57
  • Is there a way to write post meta to the 'ticket' post directly using the variable $ticket on the page itself - this way you can write to the appropriate $tickets which are called already, find the date using the $EventStartDate = date( 'dmy', $event->start ); we talked about earlier that we know works.... Thanks for all your help - tried to buy you a cocktail :-P but it says the cost of one is £0.00 on your profile link!
    – Bysander
    Commented Mar 19, 2014 at 13:29


I got round this by actually putting the function to add the meta data on the event pages themselves (in event-single.php). This means that you are not held by the date of the event at the time of the ticket being created should, for example, you change the date of the event. Each time the page is accessed the date is transferred to the tickets in question.

I stored the date metadata in yymmdd format so in fact if you order them numerically it still produces the correct result without having to re-translate into dates.

(This may not be perfect - I am a novice coder and have taught myself php through the necessity of maintaining a wordpress website)

So this is what I did.

$eventDay = date( 'ymd', $event->start );
$tixID = $ticket['id'];
$eventKey = event_date;
update_post_meta($tixID , $eventKey , $eventDay ); #This has added the event date ($eventDay) to the Ticket Meta

    //    Now we have to search to see which event is the first
$useremail = $ticket['email'];
$user = get_user_by( 'email', $useremail );
$IDuser = $user->ID;
$args = array(
              'posts_per_page' => 1,
              'post_type' => 'ticket',
              'author' => $IDuser,
              'meta_key' => 'state',
              'meta_value' => 'complete',
              'orderby' => 'event_date',
              'order' => 'ASC',
$first_post = new WP_Query($args);
//    '<pre>'.print_r($first_post).'</pre>'; /* Use this to test the array produced */
if ($first_post->have_posts()) {

    $firstID = get_the_ID(); // This gets the post number of the first 'ticket' post
    $firsteventdate = get_post_meta( $firstID, 'event_date', true); //This uses that number to extract the date of the first event.

    if ($firsteventdate == $eventDay) { ?> <b>**NEW CUSTOMER**</b> <?php } else { ?> Old
    <?php }
  • Cool. I'm glad you got it working. It was an interesting question, though I was a little lost since I didn't know the plugin in question. Commented Mar 19, 2014 at 22:17
  • Couldn't have done it without you to be honest - got me thinking in the right way to crack it! Thanks for all your help!
    – Bysander
    Commented Mar 20, 2014 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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