Change shift status to 'worked' after date passes
-
Hi Morgan,
I’m looking for a way to have the shifts change to ‘worked’ status even if employees dont punch in/out. Is there a way to do this?
The only way I can get close is with a bit of code on the shift view- that only works if the page is loaded again, then the status changes.
Any way to automate this? Thanks again for all your help. That last answer on appending data was super helpful. Happy halloween!
-
Under what circumstances do you want shifts to be marked as worked?
You could make a cron job that runs every night and marks all shifts in the past as worked, but that assumes that all shifts in the past have actually been worked…
I tried to write one but it didn’t do what I wanted. Say I wanted to trash all shifts with an additional status of ‘report-submitted’ appended to ‘worked’. So all shifts with those two categories. Could you help me a little if you have spare time for this? Always appreciated.
I’m not sure what your coding skills are like, but this might help you. I have a very specialized/customized version of this plugin for one of my clients, and for them I just wrote this: https://bitbucket.org/wpalchemist/employee-schedule-manager/src/5cd90b0bffe77d707636dabbb9affe5711f5133e/scc/archive.php?at=master&fileviewer=file-view-default
This goes through the site once a month and exports all shifts, expenses, etc. that are more than 1 year old to spreadsheets, and then deletes them. You could customize this to do what you want. The functions that will be useful to you are:
Line 29: wpaesm_archive_old_data
Line 43: wpaesm_export_and_delete (the only part of that you will need is lines 58-62
Line 92: wpaesm_find_old_shifts
Line 971: wpaesm_delete_old_posts
That will get you 95% of the way there. If you run into any hurdles customizing that to do what you need, let me know.This is perfect! Where would I stick this code, in the theme functions or in the plugin?
Don’t stick it in the plugin, because it will get deleted when the plugin is updated. You can stick it in theme functions, but only if you are using a child theme or custom theme that won’t get updated. You could also just create your own little plugin that just does this.
I’ll make up a plugin quick. So helpful, thanks so much for sharing- I’m new to php, coming from java and obj-c have been no help! Woohoo
Well, at least you have some coding experience – that certainly gives you a leg up. ??
Hi Morgan,
I’m totally lost on adding a cron job that actually changes the shifts before the current day to ‘worked’.
The code you supplied was very helpful, however i cant:
`wp_set_object_terms($post->ID, ‘worked’, ‘shift_status’, false);I really really appreciate the help!
in functions.php
/** * Schedule cron job. * * Schedule the archive event to happen daily. * * @since 2.2.0 */ function wpaesm_archive_old_data() { if ( ! wp_next_scheduled( 'wpaesm_export_and_delete' ) ) { wp_schedule_event( current_time( 'timestamp' ), 'daily', 'wpaesm_export_and_delete' ); } } add_action( 'init', 'wpaesm_archive_old_data' ); /** * Find shifts and change. * * Find all shifts on the site that is more than a day old, change to worked status * * @since 2.2.0 * */ add_action( 'wpaesm_export_and_delete', 'wpaesm_do_archiving' ); function wpaesm_do_archiving() { $before = date( 'Y-m-d', strtotime( '-1 day', time() ) ); $csv_list = array(); $deleted = array(); $oldshifts = wpaesm_find_old_shifts( $before ); if( false !== $oldshifts ) { $csv_list['shifts'] = wpaesm_export_old_shifts( $oldshifts ); $changed['shifts'] = wpaesm_delete_old_posts( $oldshifts ); } } /** * Find old shifts. * * Find all of the shifts older than the given date. * * @since 2.2.0 * * @param string Date (Y-m-d). * @return WP_Query Object Query of old shifts. */ function wpaesm_find_old_shifts( $before ) { $args = array( 'post_type' => 'shift', 'posts_per_page' => -1, 'meta_key' => '_wpaesm_date', 'meta_value' => $before, 'meta_compare' => '<=', ); $oldshifts = new WP_Query( $args ); if ( $oldshifts->have_posts() ) { return $oldshifts; } else { return false; } } /** * Change old shifts. * * Given a WP_Query object, change all of the posts in the query. * * @since 2.2.0 * @param $oldshifts WP_Query object * * @return int number of changed posts */ function wpaesm_delete_old_posts( $oldposts ) { $i = 0; while ( $oldposts->have_posts() ) : $oldposts->the_post(); $changed = wp_set_object_terms($post->ID, 'worked', 'shift_status', false); if( $changed ) { $i++; } endwhile; return $i; }
I think the problem is in this line:
$changed = wp_set_object_terms($post->ID, 'worked', 'shift_status', false);
$post isn’t set. I’m pretty sure this will work:
function wpaesm_delete_old_posts( $oldposts ) { $i = 0; while ( $oldposts->have_posts() ) : $oldposts->the_post(); $post_id = get_the_id(); $changed = wp_set_object_terms($post_id, 'worked', 'shift_status', false); if( $changed ) { $i++; } endwhile; reset_postdata(); return $i; }
That’s untested, but I think it will work
It does not seem to be registering the job. I tried running it as is and the shifts remained as is. This should be in the themes functions file, correct?
Update: the job registers, I can run it manually but with no changes made to the shifts.
$deleted = array(); should have been $changed = array(); Still nada in the way of auto-working these shifts. If anyone has any suggestions I’d appreciate it. Thanks
Can you post your complete code as you have it right now? I’ll test it out and get it working.
Wow, really? Best Friday present ever!
Here’s what I have running in the theme functions:
function wpaesm_archive_old_data_custom_recurrence( $schedules ) { $schedules['monthly'] = array( 'display' => __( 'Once a day', 'textdomain' ), 'interval' => 86400, ); return $schedules; } add_filter( 'cron_schedules', 'wpaesm_archive_old_data_custom_recurrence' ); /** * Schedule cron job. * * Schedule the archive event to happen daily. * * @since 2.2.0 */ function wpaesm_archive_old_data() { if ( ! wp_next_scheduled( 'wpaesm_export_and_delete' ) ) { wp_schedule_event( current_time( 'timestamp' ), 'daily', 'wpaesm_export_and_delete' ); } } add_action( 'init', 'wpaesm_archive_old_data' ); /** * Find shifts and change. * * Find all shifts on the site that is more than a day old, change to worked status * * @since 2.2.0 * */ add_action( 'wpaesm_export_and_delete', 'wpaesm_do_archiving' ); function wpaesm_do_archiving() { $before = date( 'm/d/Y', strtotime( '-1 day', time() ) ); $csv_list = array(); $changed = array(); $oldshifts = wpaesm_find_old_shifts( $before ); if( false !== $oldshifts ) { $csv_list['shifts'] = wpaesm_export_old_shifts( $oldshifts ); $changed['shifts'] = wpaesm_delete_old_posts( $oldshifts ); } } /** * Find old shifts. * * Find all of the shifts older than the given date. * * @since 2.2.0 * * @param string Date (Y-m-d). * @return WP_Query Object Query of old shifts. */ function wpaesm_find_old_shifts( $before ) { $args = array( 'post_type' => 'shift', 'posts_per_page' => -1, 'meta_key' => '_wpaesm_date', 'meta_value' => $before, 'meta_compare' => '<=', ); $oldshifts = new WP_Query( $args ); if ( $oldshifts->have_posts() ) { return $oldshifts; } else { return false; } } /** * Change old shifts. * * Given a WP_Query object, change all of the posts in the query. * * @since 2.2.0 * @param $oldshifts WP_Query object * * @return int number of changed posts */ function wpaesm_delete_old_posts( $oldshifts ) { $i = 0; while ( $oldshifts->have_posts() ) : $oldshifts->the_post(); $post_id = get_the_id(); $changed = wp_set_object_terms($post_id, 'worked', 'shift_status', false); if( $changed ) { $i++; } endwhile; reset_postdata(); return $i; }
- The topic ‘Change shift status to 'worked' after date passes’ is closed to new replies.