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' );