Leave a Reply

2 comments

  1. 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() ){
        $tickets->the_post();
    
        // 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' ) )
              return;
    
        // 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' );
    
  2. So,

    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.

        ## PUT EVENT DATE DETAILS ONTO TICKET POST & WORK OUT **NEW CUSTOMERS** ##
    $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()) {
        $first_post->the_post();
    
        $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.
    
    
        wp_reset_postdata();
    }
        if ($firsteventdate == $eventDay) { ?> <b>**NEW CUSTOMER**</b> <?php } else { ?> Old
        <?php }
            ?>